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): number 19 20计算指定文本单行布局下的宽度。 21 22**系统能力:** SystemCapability.ArkUI.ArkUI.Full 23 24**参数:** 25 26| 参数名 | 类型 | 必填 | 说明 | 27| ------- | ------------------------------- | ---- | --------- | 28| options | [MeasureOptions](#measureoptions) | 是 | 被计算文本描述信息。 | 29 30**返回值:** 31 32| 类型 | 说明 | 33| ------------ | --------- | 34| number | 文本宽度。<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## measure.measureTextSize<sup>10+</sup> 62 63measureTextSize(options: MeasureOptions): SizeOptions 64 65计算指定文本单行布局下的宽度。 66 67**系统能力:** SystemCapability.ArkUI.ArkUI.Full 68 69**参数:** 70 71| 参数名 | 类型 | 必填 | 说明 | 72| ------- | ------------------------------- | ---- | --------- | 73| options | [MeasureOptions](#measureoptions) | 是 | 被计算文本描述信息。 | 74 75**返回值:** 76 77| 类型 | 说明 | 78| ------------ | --------- | 79| SizeOptions | 返回文本所占布局宽度和高度。<br/>**说明:** 文本宽度以及高度返回值单位均为px。 | 80 81 82**示例:** 83 84```ts 85import measure from '@ohos.measure' 86@Entry 87@Component 88struct Index { 89 @State message: string = 'Hello World' 90 textSize : SizeOptions = measure.measureTextSize({ 91 textContent: "Hello word", 92 fontSize: '50px' 93 }) 94 build() { 95 Row() { 96 Column() { 97 Text("The width of 'Hello World': " + this.textSize.width) 98 Text("The height of 'Hello World': " + this.textSize.height) 99 } 100 .width('100%') 101 } 102 .height('100%') 103 } 104} 105``` 106 107## MeasureOptions 108 109被计算文本属性。 110 111**系统能力:** SystemCapability.ArkUI.ArkUI.Full 112 113| 名称 | 类型 | 必填 | 说明 | 114| -------------- | -------------------------------------------------------------------------------------------------- | ---- | ----------------------------------------------- | 115| textContent | string | 是 | 设置被计算文本内容。 | 116| constraintWidth<sup>10+</sup> | number \| string \| [Resource](../arkui-ts/ts-types.md#resource) | 否 | 设置被计算文本布局宽度。<br/>**说明:** 默认单位为vp | 117| fontSize | number \| string \| [Resource](../arkui-ts/ts-types.md#resource) | 否 | 设置被计算文本字体大小,fontSize为number类型时,使用fp单位。<br/>默认值:16fp。<br/>**说明:** 不支持设置百分比字符串。 | 118| fontStyle | number \| [FontStyle](../arkui-ts/ts-appendix-enums.md#fontstyle) | 否 | 设置被计算文本字体样式。<br>默认值:FontStyle.Normal | 119| fontWeight | number \| string \| [FontWeight](../arkui-ts/ts-appendix-enums.md#fontweight) | 否 | 设置被计算文本的字体粗细,number类型取值[100, 900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值的字符串形式,例如"400",以及"bold"、"bolder"、"lighter"、"regular"、"medium",分别对应FontWeight中相应的枚举值。<br/>默认值:FontWeight.Normal| 120| fontFamily | string \| [Resource](../arkui-ts/ts-types.md#resource) | 否 | 设置被计算文本字体列表。默认字体'HarmonyOS Sans',且当前只支持这种字体。| 121| letterSpacing | number \| string | 否 | 设置被计算文本字符间距。| 122| textAlign<sup>10+</sup> | number \| [TextAlign](../arkui-ts/ts-appendix-enums.md#textalign) | 否 | 设置被计算文本水平方向的对齐方式。<br/>默认值:TextAlign.Start| 123| overflow<sup>10+</sup> | number \| [TextOverflow](../arkui-ts/ts-appendix-enums.md#textoverflow) | 否 | 设置被计算文本超长时的截断方式。| 124| maxLines<sup>10+</sup> | number | 否 | 设置被计算文本最大行数。| 125| lineHeight<sup>10+</sup> | number \| string \| [Resource](../arkui-ts/ts-types.md#resource) | 否 | 设置被计算文本行高。| 126| baselineOffset<sup>10+</sup> | number \| string | 否 | 设置被计算文本基线的偏移量。<br />默认值:0 | 127| textCase<sup>10+</sup> | number \| [TextCase](../arkui-ts/ts-appendix-enums.md#textcase) | 否 | 设置被计算文本大小写。<br />默认值:TextCase.Normal | 128 129