根据给定的文字可以获得其宽度吗,未进行渲染前,如何获得文本的宽度?

根据给定的文字可以获得其宽度吗,未进行渲染前,如何获得文本的宽度。

HarmonyOS
2024-10-14 11:58:04
浏览
已于2024-10-14 11:58:20修改
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以通过getInspectorByKey获取组件信息参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-component-id-V5

demo可以参考一下:

@Entry  
@Component  
struct JSONTest {  
  @State cWidth: number = 200;  
  @State cHeight: number = 200;  
  build() {  
    Column() {  
      Column() {  
        Row() {  
          Text('isidjaslkjadskjdalksdjkajdksjdakd')  
            .id('111')  
        }  
        Button('show')  
          .type(ButtonType.Capsule)  
          .onClick(() => {  
            let jsonstr: string = getInspectorByKey('111')  
            console.log(jsonstr)  
            let obj = JSON.parse(jsonstr) as massage  
            console.log("data:" + obj.$attrs.size.height)  
  
            console.log("当前高度:" + this.cHeight + "当前高度:" + this.cHeight)  
          })  
      }  
      .backgroundColor(Color.Blue)  
  
      .width(this.cWidth)  
      .height(this.cHeight)  
  
    }  
    .width('100%')  
    .height('100%')  
  }  
}  
  
class massage{  
  $attrs : InnerMassage  
  
  constructor(inner : InnerMassage) {  
    this.$attrs = inner  
  }  
  
}  
  
class InnerMassage{  
  size : SizeMassage  
  
  constructor(size : SizeMassage) {  
    this.size = size  
  }  
}  
  
class SizeMassage{  
  height: string  
  width: string  
  
  constructor(height : string, width :string ) {  
    this.height = height  
    this.width = width  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
分享
微博
QQ
微信
回复
2024-10-14 17:56:58
相关问题
如何获取文本框里文字宽度
3118浏览 • 1回复 待解决
如何拿到文本文字宽度数值
904浏览 • 1回复 待解决
获取文本Text组件宽度
1263浏览 • 1回复 待解决
如何获取文本显示宽度和高度?
1198浏览 • 1回复 待解决
如何获取Text组件中文字宽度
3181浏览 • 1回复 待解决
有谁知道如何计算文本宽度
2980浏览 • 1回复 待解决
HarmonyOS 文件读写权限如何获得
1631浏览 • 1回复 待解决
HarmonyOS如何获得设备公网IP地址
1559浏览 • 1回复 待解决
怎么设置元素最大宽度最小宽度
926浏览 • 1回复 待解决
鸿蒙如何获得手机屏幕参数?
5263浏览 • 1回复 待解决
HarmonyOS 如何获得网络运营商类型
1445浏览 • 1回复 待解决
TextInput如何取消自动获得焦点
1355浏览 • 1回复 待解决
HarmonyOS 如何获得GMT时区时间
717浏览 • 1回复 待解决
如何监听TextInput是否获得焦点
2605浏览 • 1回复 待解决