• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.measure (Text Measurement)
2
3The **measure** module provides APIs for measuring text metrics, such as text height and width.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9
10## Modules to Import
11
12```
13import measure from '@ohos.measure'
14```
15
16## measure.measureText
17
18measureText(options: MeasureOptions): double
19
20Measures the width of the given single-line text.
21
22**System capability**: SystemCapability.ArkUI.ArkUI.Full
23
24**Parameters**
25
26| Name    | Type                             | Mandatory  | Description       |
27| ------- | ------------------------------- | ---- | --------- |
28| options | [MeasureOptions](#measureoptions) | Yes   | Information about the measured text.|
29
30**Return value**
31
32| Type         | Description      |
33| ------------  | --------- |
34| double        | Text width.<br>The unit is px.|
35
36
37**Example**
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
63Provides attributes of the measured text.
64
65**System capability**: SystemCapability.ArkUI.ArkUI.Full
66
67| Name          | Type                                                                                               | Mandatory| Description                     |
68| -------------- | -------------------------------------------------------------------------------------------------- | ---- | ----------------------------------------------- |
69| textContent    | string                                                                                             | Yes  | Content of the measured text.                                 |
70| fontSize       | number \| string \| [Resource](../arkui-ts/ts-types.md#resource)               | No  | Font size of the measured text. If the value is of the number type, the unit fp is used.<br>Default value: **16fp**<br>**NOTE**<br>The value cannot be a percentage.   |
71| fontStyle      | number \| [FontStyle](../arkui-ts/ts-appendix-enums.md#fontstyle)                        | No  | Font style of the measured text.<br>Default value: **FontStyle.Normal**           |
72| fontWeight     | number \| string \| [FontWeight](../arkui-ts/ts-appendix-enums.md#fontweight)  | No  | Font width of the measured text. For the number type, the value ranges from 100 to 900, at an interval of 100. The default value is **400**. A larger value indicates a heavier font weight. The string type supports only the string of the number type, for example, **400**, **"bold"**, **"bolder"**, **"lighter"**, **"regular"**, and **"medium"**, which correspond to the enumerated values in **FontWeight**.<br>Default value: **FontWeight.Normal**|
73| fontFamily     | string \| [Resource](../arkui-ts/ts-types.md#resource)                                   | No  | Font family of the measured text. Default value: **'HarmonyOS Sans'**<br>Only the default font is supported.|
74| letterSpacing  | number \| string                                                                         | No  | Letter spacing of the measured text.|
75