参考:文档中心
本模块提供文本宽度、高度等相关计算
import { MeasureText } from '@kit.ArkUI'
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%')
}
}
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%')
}
}