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%')
  }
}
相关推荐
Math_teacher_fan10 小时前
Flutter 跨平台开发实战:鸿蒙与音乐律动艺术(六)、Lissajous 利萨茹曲线:频率耦合的轨迹艺术
flutter·ui·数学建模·华为·harmonyos·鸿蒙系统
xmdy586612 小时前
Flutter+开源鸿蒙实战|智安盾电商溯源平台Day3 溯源查询逻辑+鸿蒙网络请求适配
flutter·开源·harmonyos
maaath12 小时前
【maaath】Flutter 跨平台日历日程应用开发实战
flutter·华为·harmonyos
LeesonWong13 小时前
架构困境与四层结构化设计
harmonyos
梦想不只是梦与想14 小时前
鸿蒙 应用市场更新功能:版本检测与更新提醒
harmonyos·鸿蒙·版本更新
xmdy586614 小时前
Flutter+开源鸿蒙实战|智安盾电商溯源平台Day2 首页+核心入口UI开发(鸿蒙多端适配)
flutter·开源·harmonyos
nashane18 小时前
HarmonyOS 6学习:HAR包与HSP包的选择与优化指南
学习·华为·harmonyos·harmonyos 5
全栈若城18 小时前
自定义 TabBar 实战:浮动标签栏与舵式标签栏
harmonyos·harmonyos6·三方库开发
maaath19 小时前
【maaath】Flutter for OpenHarmony 学习答题应用实战开发
学习·flutter·华为·harmonyos
李游Leo19 小时前
别再拼 JSON 了:HarmonyOS UDMF 跨应用数据流转实践
harmonyos