解决措施
使用measureText计算指定文本单行布局的宽度。
代码示例
import { MeasureText } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
@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%')
}
}
参考链接
"文本计算" (https://link.segmentfault.com/?enc=HYWS%2FZqU1ZIYJo0ghcSACg%3D%3D.Ac0NPxdxyW8mSeZFxXtBgFo%2BhEbU1x%2FSzoMy80rxyLTl9kzlfYgArFFpFWFMmGzoyCpJH7Vf3EwsY2vQoAvE6f31P4%2FGiKAF7R3LBRpIJTaCDtLdE8hea%2BGyULbZoLDG9krX%2FkComevXBrs384Dv3w%3D%3D)