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> Since API version 12, you can use the **getMeasureUtils** API in **UIContext** to obtain the [MeasureUtils](js-apis-arkui-UIContext.md#measureutils12)r object associated with the current UI context. 10 11## Modules to Import 12 13```ts 14import { MeasureText } from '@kit.ArkUI' 15``` 16 17## MeasureText.measureText 18 19measureText(options: MeasureOptions): number 20 21Measures the width of the given single-line text. 22 23**Atomic service API**: This API can be used in atomic services since API version 12. 24 25**System capability**: SystemCapability.ArkUI.ArkUI.Full 26 27**Parameters** 28 29| Name | Type | Mandatory | Description | 30| ------- | ------------------------------- | ---- | --------- | 31| options | [MeasureOptions](#measureoptions) | Yes | Information about the measured text. | 32 33**Return value** 34 35| Type | Description | 36| ------------ | --------- | 37| number | Text width.<br>Unit: px | 38 39 40**Example** 41 42```ts 43import { MeasureText } from '@kit.ArkUI' 44 45@Entry 46@Component 47struct Index { 48 @State textWidth: number = MeasureText.measureText({ 49 textContent: "Hello word", 50 fontSize: '50px' 51 }) 52 53 build() { 54 Row() { 55 Column() { 56 Text(`The width of 'Hello World': ${this.textWidth}`) 57 } 58 .width('100%') 59 } 60 .height('100%') 61 } 62} 63``` 64 65## MeasureText.measureTextSize<sup>10+</sup> 66 67measureTextSize(options: MeasureOptions): SizeOptions 68 69Measures the width and height of the given single-line text. 70 71**Atomic service API**: This API can be used in atomic services since API version 12. 72 73**System capability**: SystemCapability.ArkUI.ArkUI.Full 74 75**Parameters** 76 77| Name | Type | Mandatory | Description | 78| ------- | ------------------------------- | ---- | --------- | 79| options | [MeasureOptions](#measureoptions) | Yes | Information about the measured text. | 80 81**Return value** 82 83| Type | Description | 84| ------------ | --------- | 85| [SizeOptions](arkui-ts/ts-types.md#sizeoptions) | Layout width and height occupied by the text.<br>**NOTE**<br>The return values for text width and height are both in px. | 86 87 88**Example** 89 90```ts 91import { MeasureText } from '@kit.ArkUI' 92 93@Entry 94@Component 95struct Index { 96 textSize : SizeOptions = MeasureText.measureTextSize({ 97 textContent: "Hello word", 98 fontSize: '50px' 99 }) 100 build() { 101 Row() { 102 Column() { 103 Text(`The width of 'Hello World': ${this.textSize.width}`) 104 Text(`The height of 'Hello World': ${this.textSize.height}`) 105 } 106 .width('100%') 107 } 108 .height('100%') 109 } 110} 111``` 112 113## MeasureOptions 114 115Provides attributes of the measured text. 116 117**Atomic service API**: This API can be used in atomic services since API version 12. 118 119**System capability**: SystemCapability.ArkUI.ArkUI.Full 120 121| Name | Type | Mandatory | Description | 122| -------------- | -------------------------------------------------------------------------------------------------- | ---- | ----------------------------------------------- | 123| textContent | string \| [Resource](arkui-ts/ts-types.md#resource) | Yes | Content of the measured text. | 124| constraintWidth<sup>10+</sup> | number \| string \| [Resource](arkui-ts/ts-types.md#resource) | No | Layout width of the measured text.<br>**NOTE**<br>The default unit is vp. The value cannot be a percentage. If this parameter is not set, the value of **SizeOption** is the maximum width allowed for the single-line text. | 125| fontSize | number \| string \| [Resource](arkui-ts/ts-types.md#resource) | No | Font size of the text to be measured. When **fontSize** is of the number type, the unit is vp.<br>Default value: **16**<br>**NOTE**<br>The value cannot be a percentage.<br>Since API version 12, the fp unit is used when **fontSize** is of the number type. | 126| fontStyle | number \| [FontStyle](arkui-ts/ts-appendix-enums.md#fontstyle) | No | Font style of the measured text.<br>Default value: **FontStyle.Normal** | 127| 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. A larger value indicates a heavier font weight. The default value is **400**. For the string type, only strings of the number type are supported, for example, **400**, **"bold"**, **"bolder"**, **"lighter"**, **"regular"**, and **"medium"**, which correspond to the enumerated values in **FontWeight**.<br>Default value: **FontWeight.Normal**| 128| 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.| 129| letterSpacing | number \| string | No | Letter spacing of the measured text.| 130| textAlign<sup>10+</sup> | number \| [TextAlign](arkui-ts/ts-appendix-enums.md#textalign) | No | Horizontal alignment mode of the measured text.<br>Default value: **TextAlign.Start**| 131| overflow<sup>10+</sup> | number \| [TextOverflow](arkui-ts/ts-appendix-enums.md#textoverflow) | No | Display mode when the measured text is too long.| 132| maxLines<sup>10+</sup> | number | No | Maximum number of lines in the measured text.| 133| lineHeight<sup>10+</sup> | number \| string \| [Resource](arkui-ts/ts-types.md#resource) | No | Line height of the measured text.| 134| baselineOffset<sup>10+</sup> | number \| string | No | Baseline offset of the measured text.<br>Default value: **0** | 135| textCase<sup>10+</sup> | number \| [TextCase](arkui-ts/ts-appendix-enums.md#textcase) | No | Case of the measured text.<br>Default value: **TextCase.Normal** | 136| textIndent<sup>11+</sup> | number \| string | No | Indentation of the first line.<br>Default value: **0** | 137| wordBreak<sup>11+</sup> | [WordBreak](arkui-ts/ts-appendix-enums.md#wordbreak11) | No | Line break rule.<br>Default value: **WordBreak.BREAK_WORD**<br>**NOTE**<br>When used with **{overflow: TextOverflow.Ellipsis}** and **maxLines**, **WordBreak.BREAK_ALL** can insert line breaks between letters when overflow occurs and display excess content with an ellipsis (...). | 138