• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.measure (文本计算)
2
3本模块提供文本宽度、高度等相关计算。
4
5> **说明**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10## 导入模块
11
12```
13import measure from '@ohos.measure'
14```
15
16## measure.measureText
17
18measureText(options: MeasureOptions): double
19
20计算指定文本单行布局下的宽度。
21
22**系统能力:** SystemCapability.ArkUI.ArkUI.Full
23
24**参数:**
25
26| 参数名     | 类型                              | 必填   | 说明        |
27| ------- | ------------------------------- | ---- | --------- |
28| options | [MeasureOptions](#measureoptions) | 是    | 被计算文本描述信息。 |
29
30**返回值:**
31
32| 类型          | 说明       |
33| ------------  | --------- |
34| double        | 文本宽度。<br/>**说明:** 单位px。 |
35
36
37**示例:**
38
39```ts
40import measure from '@ohos.measure'
41@Entry
42@Component
43struct Index {
44  @State message: string = 'Hello World'
45  @State textWidth : number = measure.measureText({
46    textContent: "Hello word",
47    fontSize: '50px'
48  })
49  build() {
50    Row() {
51      Column() {
52        Text("The width of 'Hello World': " + this.textWidth)
53      }
54      .width('100%')
55    }
56    .height('100%')
57  }
58}
59```
60
61## MeasureOptions
62
63被计算文本属性。
64
65**系统能力:** SystemCapability.ArkUI.ArkUI.Full
66
67| 名称           | 类型                                                                                                | 必填 | 说明                      |
68| -------------- | -------------------------------------------------------------------------------------------------- | ---- | ----------------------------------------------- |
69| textContent    | string                                                                                             | 是   | 设置被计算文本内容。                                  |
70| fontSize       | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](../arkui-ts/ts-types.md#resource)               | 否   | 设置被计算文本字体大小,fontSize为number类型时,使用fp单位。<br/>默认值:16fp。<br/>**说明:**不支持设置百分比字符串。    |
71| fontStyle      | number&nbsp;\|&nbsp;[FontStyle](../arkui-ts/ts-appendix-enums.md#fontstyle)                        | 否   | 设置被计算文本字体样式。<br>默认值:FontStyle.Normal            |
72| 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|
73| fontFamily     | string&nbsp;\|&nbsp;[Resource](../arkui-ts/ts-types.md#resource)                                   | 否   | 设置被计算文本字体列表。默认字体'HarmonyOS Sans',且当前只支持这种字体。|
74| letterSpacing  | number&nbsp;\|&nbsp;string                                                                         | 否   | 设置被计算文本字符间距。|
75
76