参考:文档中心
@ohos.measure (文本计算)
本模块提供文本宽度、高度等相关计算
从API version 12开始,可以通过UIContext中的getMeasureUtils方法获取当前UI上下文关联的MeasureUtils实例
如需更多测算文本参数,建议使用图形对应测算接口Paragraph接口。
导入模块
import { MeasureText } from '@kit.ArkUI'
MeasureText.measureText
measureText(options: MeasureOptions): number
计算指定文本的宽度。
示例:
import { MeasureText } from '@kit.ArkUI'
@Entry
@Component
struct Index {
@State textWidth: number = MeasureText.measureText({
textContent: "Hello word",
fontSize: '50px'
})
build() {
Row() {
Column() {
Text(`The width of 'Hello World': ${this.textWidth}`)
}
.width('100%')
}
.height('100%')
}
}
MeasureText.measureTextSize
measureTextSize(options: MeasureOptions): SizeOptions
计算指定文本的宽度和高度。
import { MeasureText } from '@kit.ArkUI'
@Entry
@Component
struct Index {
textSize : SizeOptions = MeasureText.measureTextSize({
textContent: "Hello word",
fontSize: '50px'
})
build() {
Row() {
Column() {
Text(`The width of 'Hello World': ${this.textSize.width}`)
Text(`The height of 'Hello World': ${this.textSize.height}`)
}
.width('100%')
}
.height('100%')
}
}