13、鸿蒙学习——文本计算

参考:文档中心

@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%')
  }
}
相关推荐
安卓开发者8 小时前
鸿蒙NEXT跨设备通信:掌握URPC,实现远程程序调用
华为·harmonyos
程序员潘Sir10 小时前
鸿蒙应用开发从入门到实战(十七):ArkUI组件List&列表布局
harmonyos·鸿蒙
bst@微胖子11 小时前
鸿蒙实现滴滴出行项目之侧边抽屉栏以及权限以及搜索定位功能
android·华为·harmonyos
爱笑的眼睛111 天前
深入浅出 HarmonyOS 应用开发:ArkTS 语法精要与实践
华为·harmonyos
爱笑的眼睛111 天前
HarmonyOS应用开发深度解析:ArkTS语法精要与UI组件实践
华为·harmonyos
Kisang.1 天前
【HarmonyOS】消息通知
华为·harmonyos
安卓开发者1 天前
鸿蒙NEXT网络通信实战:使用HTTP协议进行网络请求
网络·http·harmonyos
爱笑的眼睛111 天前
HarmonyOS ArkTS深度解析:构建高性能声明式UI应用
华为·harmonyos
TiZizzz1 天前
HarmonyOS应用开发 - strip编译配置优先级
华为·harmonyos