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%')
  }
}
相关推荐
被开发耽误的大厨1 小时前
鸿蒙项目篇-22-项目功能结构说明-写子页面和导航页面
android·华为·harmonyos·鸿蒙
祥睿夫子17 小时前
鸿蒙 ArkTS 类继承与多态实战:从语法到员工工资计算全指南
harmonyos
shenshizhong17 小时前
看懂鸿蒙系统源码 比较重要的知识点
android·harmonyos
特立独行的猫a19 小时前
强大的鸿蒙HarmonyOS网络调试工具PageSpy 介绍及使用
网络·华为·harmonyos
ChinaDragon19 小时前
HarmonyOS:在NDK工程中使用预构建库
harmonyos
程序员潘Sir1 天前
鸿蒙应用开发从入门到实战(三):第一个鸿蒙应用
harmonyos·鸿蒙
安卓开发者1 天前
鸿蒙NEXT中SQLite数据库全面实战指南
数据库·sqlite·harmonyos
HarderCoder1 天前
仓颉语言Option 的“问号”全景图—— 一个 `?` 与 `.` `()` `[]` `{}` 的组合写法
harmonyos
威哥爱编程1 天前
HarmonyOS 5.1手势事件详解
harmonyos
HarderCoder2 天前
使用仓颉语言理解 SOLID 原则:概念、实战与踩坑总结
harmonyos