导入模块

import measure from '@ohos.measure'

measure.measureText

measureText(options: MeasureOptions): number

计算指定文本单行布局下的宽度。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名

类型

必填

说明

options

MeasureOptions


被计算文本描述信息。

返回值:

类型

说明

number

文本宽度。说明: 单位px。

示例:

import measure from '@ohos.measure'
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  @State textWidth : number = measure.measureText({
    textContent: "Hello word",
    fontSize: '50px'
  })
  build() {
    Row() {
      Column() {
        Text("The width of 'Hello World': " + this.textWidth)
      }
      .width('100%')
    }
    .height('100%')
  }
}

MeasureOptions

被计算文本属性。

系统能力: SystemCapability.ArkUI.ArkUI.Full

鸿蒙UI界面——@ohos.measure (文本计算)_鸿蒙