如何获取文本框里的文字宽度

如何获取文本框里的文字宽度

HarmonyOS
2024-01-21 12:21:18
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
newyearday

使用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%') 
  } 
}

参考链接:

文本计算

分享
微博
QQ
微信
回复
2024-01-22 14:53:31
提问
该提问已有0人参与 ,帮助了0人