HarmonyOS 文本计算不准?

定位到问题点是计算的时候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%')
  }
}
阅读 560
1 个回答

measure.measureText 计算的文本宽度单位为px,此外还与fontSize() fontWeight()有关

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进