143 行
4.1 KiB
Plaintext
143 行
4.1 KiB
Plaintext
<template>
|
|
<view style="flex-grow: 1;">
|
|
<view class="box">
|
|
<text class="common" style="text-align: left;">text-align: left</text>
|
|
<text class="common" style="text-align: center;">text-align: center</text>
|
|
<text class="common2" style="text-align: center;">text-align: center</text>
|
|
<text class="common" style="text-align: right;">text-align: right</text>
|
|
</view>
|
|
<text class="uni-title-text">拍平版本</text>
|
|
<view class="box">
|
|
<text class="common" style="text-align: left;" flatten>text-align: left</text>
|
|
<text class="common" style="text-align: center;" flatten>text-align: center</text>
|
|
<text class="common2" style="text-align: center;" flatten>text-align: center</text>
|
|
<text class="common" style="text-align: right;" flatten>text-align: right</text>
|
|
</view>
|
|
|
|
<view class="uni-common-mt">
|
|
<text class="uni-title-text">setProperty 设置与 getPropertyValue 获取</text>
|
|
</view>
|
|
|
|
<view class="common-box">
|
|
<!-- 普通版本 -->
|
|
<view class="uni-common-mt">
|
|
<text class="uni-title-text">text-align</text>
|
|
<text class="uni-info">设置值: {{data.textAlign}}</text>
|
|
<text class="uni-info">获取值: {{data.textAlignActual}}</text>
|
|
<view class="test-box">
|
|
<text ref="textRef" class="common-text test-text" :style="{ textAlign: data.textAlign }">当前 text-align: {{data.textAlign}}</text>
|
|
</view>
|
|
</view>
|
|
|
|
<!-- 拍平版本 -->
|
|
<view class="uni-common-mt">
|
|
<text class="uni-title-text">拍平</text>
|
|
<text class="uni-info">设置值: {{data.textAlign}}</text>
|
|
<text class="uni-info">获取值: {{data.textAlignActualFlat}}</text>
|
|
<view class="test-box">
|
|
<text ref="textRefFlat" class="common-text test-text-flatten" :style="{ textAlign: data.textAlign }" flatten>当前 text-align: {{data.textAlign}}</text>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
|
|
<view class="uni-common-mt uni-common-mb">
|
|
<text class="uni-tips">第一个枚举值,'' (空字符串) - 空值情况</text>
|
|
<enum-data :items="textAlignEnum" title="text-align 枚举值" @change="radioChangeTextAlign" :compact="true"></enum-data>
|
|
<input-data :defaultValue="data.textAlign" title="text-align 自定义值" type="text" @confirm="inputChangeTextAlign"></input-data>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script setup lang="uts">
|
|
import { ItemType } from '@/components/enum-data/enum-data-types'
|
|
|
|
const textAlignEnum: ItemType[] = [
|
|
{ value: 0, name: '' },
|
|
{ value: 1, name: 'left' },
|
|
{ value: 2, name: 'center' },
|
|
{ value: 3, name: 'right' }
|
|
]
|
|
|
|
const data = reactive({
|
|
textAlign: 'left',
|
|
textAlignActual: '',
|
|
textAlignActualFlat: ''
|
|
})
|
|
const textRef = ref(null as UniTextElement | null)
|
|
const textRefFlat = ref(null as UniTextElement | null)
|
|
|
|
const getPropertyValues = () => {
|
|
data.textAlignActual = textRef.value?.style.getPropertyValue('text-align') ?? ''
|
|
data.textAlignActualFlat = textRefFlat.value?.style.getPropertyValue('text-align') ?? ''
|
|
}
|
|
|
|
|
|
const changeTextAlign = (value: string) => {
|
|
data.textAlign = value
|
|
textRef.value?.style.setProperty('text-align', value)
|
|
textRefFlat.value?.style.setProperty('text-align', value)
|
|
// 使用 nextTick 确保样式已应用后再获取值
|
|
nextTick(() => {
|
|
getPropertyValues()
|
|
})
|
|
}
|
|
|
|
const radioChangeTextAlign = (index: number) => {
|
|
const selectedItem = textAlignEnum.find((item): boolean => item.value === index)
|
|
if (selectedItem != null) {
|
|
changeTextAlign(selectedItem.name)
|
|
}
|
|
}
|
|
|
|
const inputChangeTextAlign = (value: string) => {
|
|
changeTextAlign(value)
|
|
}
|
|
|
|
onReady(() => {
|
|
getPropertyValues()
|
|
})
|
|
|
|
defineExpose({
|
|
radioChangeTextAlign,
|
|
data
|
|
})
|
|
</script>
|
|
|
|
<style>
|
|
.common {
|
|
width: 250px;
|
|
font-size: 20px;
|
|
}
|
|
|
|
.common2 {
|
|
min-width: 250px;
|
|
font-size: 20px;
|
|
}
|
|
|
|
.common-text{
|
|
width: 100px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.box{
|
|
height: 150px;
|
|
background-color: gray;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
|
|
.common-box{
|
|
flex-direction: row;
|
|
justify-content: space-around;
|
|
}
|
|
|
|
.test-box {
|
|
width: 180px;
|
|
height: 80px;
|
|
background-color: gray;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
|
|
</style>
|