定位到问题点是计算的时候fontsize必须带px,否则就会计算不准
代码如下
import measure from '@ohos.measure'
@Entry
@Component
struct Index {
textSize : SizeOptions = measure.measureTextSize({
textContent: "Hello word",
fontSize: '50px'
})
build() {
Row() {
Column() {
Text(`The width of 'Hello World': ${this.textSize.width}`).width(this.textWidth)
Text(`The height of 'Hello World': ${this.textSize.height}`).width(this.textWidth)
}
.width('100%')
}
.height('100%')
}
}
measure.measureText 计算的文本宽度单位为px,此外还与fontSize() fontWeight()有关