1e41f4b71Sopenharmony_ci# @ohos.measure (文本计算)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块提供文本宽度、高度等相关计算。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci> 
9e41f4b71Sopenharmony_ci> 该模块不支持在[UIAbility](../apis-ability-kit/js-apis-app-ability-uiAbility.md)的文件声明处使用,即不能在UIAbility的生命周期中调用,需要在创建组件实例后使用。
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> 从API version 12开始,可以通过UIContext中的getMeasureUtils方法获取当前UI上下文关联的[MeasureUtils](js-apis-arkui-UIContext.md#measureutils12)实例
12e41f4b71Sopenharmony_ci>
13e41f4b71Sopenharmony_ci> 如需更多测算文本参数,建议使用图形对应测算接口[Paragraph](../apis-arkgraphics2d/js-apis-graphics-text.md#paragraph)接口。
14e41f4b71Sopenharmony_ci>
15e41f4b71Sopenharmony_ci> 为保障时序正确,推荐需要测算文本的开发者自行监听字体缩放变化,保证测算结果的准确性。
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## 导入模块
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci```ts
20e41f4b71Sopenharmony_ciimport { MeasureText } from '@kit.ArkUI'
21e41f4b71Sopenharmony_ci```
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci## MeasureText.measureText
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_cimeasureText(options: MeasureOptions): number
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci计算指定文本的宽度。
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**参数:**
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci| 参数名     | 类型                              | 必填   | 说明        |
36e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | --------- |
37e41f4b71Sopenharmony_ci| options | [MeasureOptions](#measureoptions) | 是    | 被计算文本描述信息。 |
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**返回值:**
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci| 类型          | 说明       |
42e41f4b71Sopenharmony_ci| ------------  | --------- |
43e41f4b71Sopenharmony_ci| number        | 文本宽度。<br/>单位:px |
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**示例:**
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci> **说明**
49e41f4b71Sopenharmony_ci>
50e41f4b71Sopenharmony_ci>推荐通过[UIContext](js-apis-arkui-UIContext.md#uicontext)中的[getMeasureUtils](./js-apis-arkui-UIContext.md#getmeasureutils12)方法获取当前UI上下文关联的[MeasureUtils](js-apis-arkui-UIContext.md#measureutils12)实例
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci```ts
53e41f4b71Sopenharmony_ciimport { MeasureText } from '@kit.ArkUI'
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci@Entry
56e41f4b71Sopenharmony_ci@Component
57e41f4b71Sopenharmony_cistruct Index {
58e41f4b71Sopenharmony_ci  @State textWidth: number = MeasureText.measureText({ // 建议使用 this.getUIContext().getMeasureUtils().measureText()接口
59e41f4b71Sopenharmony_ci    textContent: "Hello word",
60e41f4b71Sopenharmony_ci    fontSize: '50px'
61e41f4b71Sopenharmony_ci  })
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci  build() {
64e41f4b71Sopenharmony_ci    Row() {
65e41f4b71Sopenharmony_ci      Column() {
66e41f4b71Sopenharmony_ci        Text(`The width of 'Hello World': ${this.textWidth}`)
67e41f4b71Sopenharmony_ci      }
68e41f4b71Sopenharmony_ci      .width('100%')
69e41f4b71Sopenharmony_ci    }
70e41f4b71Sopenharmony_ci    .height('100%')
71e41f4b71Sopenharmony_ci  }
72e41f4b71Sopenharmony_ci}
73e41f4b71Sopenharmony_ci```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci## MeasureText.measureTextSize<sup>10+</sup>
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_cimeasureTextSize(options: MeasureOptions): SizeOptions
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci计算指定文本的宽度和高度。
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci**参数:**
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci| 参数名     | 类型                              | 必填   | 说明        |
88e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | --------- |
89e41f4b71Sopenharmony_ci| options | [MeasureOptions](#measureoptions) | 是    | 被计算文本描述信息。 |
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci**返回值:**
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci| 类型          | 说明       |
94e41f4b71Sopenharmony_ci| ------------  | --------- |
95e41f4b71Sopenharmony_ci| [SizeOptions](arkui-ts/ts-types.md#sizeoptions)  | 返回文本所占布局宽度和高度。<br/>**说明:**  <br/>文本宽度以及高度返回值单位均为px。 |
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci**示例:**
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci> **说明**
101e41f4b71Sopenharmony_ci>
102e41f4b71Sopenharmony_ci>推荐通过[UIContext](js-apis-arkui-UIContext.md#uicontext)中的[getMeasureUtils](./js-apis-arkui-UIContext.md#getmeasureutils12)方法获取当前UI上下文关联的[MeasureUtils](js-apis-arkui-UIContext.md#measureutils12)实例
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci```ts
105e41f4b71Sopenharmony_ciimport { MeasureText } from '@kit.ArkUI'
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci@Entry
108e41f4b71Sopenharmony_ci@Component
109e41f4b71Sopenharmony_cistruct Index {
110e41f4b71Sopenharmony_ci  textSize : SizeOptions = MeasureText.measureTextSize({ // 建议使用 this.getUIContext().getMeasureUtils().measureText()接口
111e41f4b71Sopenharmony_ci    textContent: "Hello word",
112e41f4b71Sopenharmony_ci    fontSize: '50px'
113e41f4b71Sopenharmony_ci  })
114e41f4b71Sopenharmony_ci  build() {
115e41f4b71Sopenharmony_ci    Row() {
116e41f4b71Sopenharmony_ci      Column() {
117e41f4b71Sopenharmony_ci        Text(`The width of 'Hello World': ${this.textSize.width}`)
118e41f4b71Sopenharmony_ci        Text(`The height of 'Hello World': ${this.textSize.height}`)
119e41f4b71Sopenharmony_ci      }
120e41f4b71Sopenharmony_ci      .width('100%')
121e41f4b71Sopenharmony_ci    }
122e41f4b71Sopenharmony_ci    .height('100%')
123e41f4b71Sopenharmony_ci  }
124e41f4b71Sopenharmony_ci}
125e41f4b71Sopenharmony_ci```
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci## MeasureOptions
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci被计算文本属性。
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci| 名称           | 类型                                                                                                | 必填 | 说明                      |
136e41f4b71Sopenharmony_ci| -------------- | -------------------------------------------------------------------------------------------------- | ---- | ----------------------------------------------- |
137e41f4b71Sopenharmony_ci| textContent | string&nbsp;\|&nbsp;[Resource](arkui-ts/ts-types.md#resource)                                                                                             | 是   | 设置被计算文本内容。                                  |
138e41f4b71Sopenharmony_ci| constraintWidth<sup>10+</sup> | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](arkui-ts/ts-types.md#resource)   | 否   | 设置被计算文本布局宽度。<br/>**说明:** <br/>默认单位为vp,不支持设置百分比字符串。若不设置,则文本SizeOption宽度为单行布局所占最大宽度值,若设置则为设置值。                             |
139e41f4b71Sopenharmony_ci| fontSize       | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](arkui-ts/ts-types.md#resource)               | 否   | 设置被计算文本字体大小,fontSize为number类型时,使用vp单位。<br/>默认值:16<br/>**说明:** <br/>不支持设置百分比字符串。<br/>从API version 12开始,fontSize为number类型时,使用fp单位。    |
140e41f4b71Sopenharmony_ci| fontStyle      | number&nbsp;\|&nbsp;[FontStyle](arkui-ts/ts-appendix-enums.md#fontstyle)                        | 否   | 设置被计算文本字体样式。<br>默认值:FontStyle.Normal            |
141e41f4b71Sopenharmony_ci| fontWeight     | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[FontWeight](arkui-ts/ts-appendix-enums.md#fontweight)  | 否   | 设置被计算文本的字体粗细,number类型取值[100,&nbsp;900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值的字符串形式,例如"400",以及"bold"、"bolder"、"lighter"、"regular"、"medium",分别对应FontWeight中相应的枚举值。<br/>默认值:FontWeight.Normal|
142e41f4b71Sopenharmony_ci| fontFamily     | string&nbsp;\|&nbsp;[Resource](arkui-ts/ts-types.md#resource)                                   | 否   | 设置被计算文本字体列表。默认字体'HarmonyOS Sans',且当前只支持这种字体。|
143e41f4b71Sopenharmony_ci| letterSpacing  | number&nbsp;\|&nbsp;string                                                                         | 否   | 设置被计算文本字符间距。|
144e41f4b71Sopenharmony_ci| textAlign<sup>10+</sup>  | number&nbsp;\|&nbsp;[TextAlign](arkui-ts/ts-appendix-enums.md#textalign)              | 否   | 设置被计算文本水平方向的对齐方式。<br/>默认值:TextAlign.Start|
145e41f4b71Sopenharmony_ci| overflow<sup>10+</sup>  | number&nbsp;\|&nbsp;[TextOverflow](arkui-ts/ts-appendix-enums.md#textoverflow)         | 否   | 设置被计算文本超长时的截断方式。|
146e41f4b71Sopenharmony_ci| maxLines<sup>10+</sup>  | number                                                                                    | 否   | 设置被计算文本最大行数。|
147e41f4b71Sopenharmony_ci| lineHeight<sup>10+</sup>  | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](arkui-ts/ts-types.md#resource)    | 否   | 设置被计算文本行高。|
148e41f4b71Sopenharmony_ci| baselineOffset<sup>10+</sup>  | number&nbsp;\|&nbsp;string                                                          | 否   | 设置被计算文本基线的偏移量。<br />默认值:0 |
149e41f4b71Sopenharmony_ci| textCase<sup>10+</sup>  | number&nbsp;\|&nbsp;[TextCase](arkui-ts/ts-appendix-enums.md#textcase)                 | 否   | 设置被计算文本大小写。<br />默认值:TextCase.Normal |
150e41f4b71Sopenharmony_ci| textIndent<sup>11+</sup> | number&nbsp;\|&nbsp;string  | 否  | 设置首行文本缩进,默认值0。 |
151e41f4b71Sopenharmony_ci| wordBreak<sup>11+</sup> | [WordBreak](arkui-ts/ts-appendix-enums.md#wordbreak11) | 否   | 设置断行规则。 <br />默认值:WordBreak.BREAK_WORD <br/>**说明:** <br/>WordBreak.BREAK_ALL与{overflow:&nbsp;TextOverflow.Ellipsis},`maxLines`组合使用可实现英文单词按字母截断,超出部分以省略号显示。 |