1# Text 2 3The **Text** component is used to display a piece of textual information. 4 5> **NOTE** 6> 7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11 12This component can contain the [Span](ts-basic-components-span.md), [ImageSpan](ts-basic-components-imagespan.md), [\<SymbolSpan>](ts-basic-components-symbolSpan.md), and [\<ContainerSpan>](ts-basic-components-containerspan.md) child components. 13 14## APIs 15 16Text(content?: string | Resource , value?: TextOptions) 17 18**Widget capability**: This API can be used in ArkTS widgets since API version 9. 19 20**Atomic service API**: This API can be used in atomic services since API version 11. 21 22**Parameters** 23 24| Name | Type | Mandatory | Description | 25| -------- | -------- | -------- | -------- | 26| content | string \| [Resource](ts-types.md#resource) | No | Text content. This parameter does not take effect if the component contains a **Span** child component and does not have any [styled string](ts-universal-styled-string.md#styled-string) configured. In this case, the span content is displayed, and the style of the component does not take effect.<br>Default value: **' '** | 27| value<sup>11+</sup> | [TextOptions](#textoptions11) | No | Initialization options of the component.| 28 29## Attributes 30 31In addition to the [universal attributes](ts-universal-attributes-size.md) and [universal text attributes](ts-universal-attributes-text-style.md), the following attributes are supported. 32 33### textAlign 34 35textAlign(value: TextAlign) 36 37Sets the horizontal alignment mode of the text. 38 39The text takes up the full width of the **Text** component. 40 41To set vertical alignment for the text, use the [align](ts-universal-attributes-location.md) attribute. The **align** attribute alone does not control the horizontal position of the text. In other words, **Alignment.TopStart**, **Alignment.Top**, and **Alignment.TopEnd** produce the same effect, top-aligning the text; **Alignment.Start**, **Alignment.Center**, and **Alignment.End** produce the same effect, centered-aligning the text vertically; **Alignment.BottomStart**, **Alignment.Bottom**, and **Alignment.BottomEnd** produce the same effect, bottom-aligning the text. Yet, it can work with the **textAlign** attribute to jointly determine the horizontal position of the text. 42 43When **textAlign** is set to **TextAlign.JUSTIFY**, you must set the [wordBreak](#wordbreak11) attribute, and the text in the last line is horizontally aligned with the start edge. 44 45**Widget capability**: This API can be used in ArkTS widgets since API version 9. 46 47**Atomic service API**: This API can be used in atomic services since API version 11. 48 49**System capability**: SystemCapability.ArkUI.ArkUI.Full 50 51**Parameters** 52 53| Name | Type | Mandatory | Description | 54| ------ | ------------------------------------------- | ---- | ---------------------------------------------------------- | 55| value | [TextAlign](ts-appendix-enums.md#textalign) | Yes | Horizontal alignment of the text.<br>Default value: **TextAlign.Start** | 56 57### textOverflow 58 59textOverflow(value: { overflow: TextOverflow }) 60 61Sets the display mode when the text is too long. 62 63Text is clipped at the transition between words. To clip text in the middle of a word, add **\u200B** between characters. Since API version 11, preferably set the **wordBreak** attribute to **WordBreak.BREAK_ALL** to achieve the same purpose. For details, see [Example](#example-4). 64 65If **overflow** is set to **TextOverflow.None**, **TextOverflow.Clip**, or **TextOverflow.Ellipsis**, this attribute must be used with **maxLines** for the settings to take effect. **TextOverflow.None** produces the same effect as **TextOverflow.Clip**. 66 67If **overflow** is set to **TextOverflow.MARQUEE**, the text scrolls in a line, and neither **maxLines** nor **copyOption** takes effect. The **textAlign** attribute takes effect only when the text is not scrollable. With **overflow** set to **TextOverflow.MARQUEE**, the **clip** attribute is set to **true** by default. **TextOverflow.MARQUEE** is not available for [CustomSpan](ts-universal-styled-string.md#customspan) of the styled string. 68 69Since API version 12, **TextOverflow.MARQUEE** is available for the **ImageSpan** component, where the text and images are displayed in scrolling mode in a line. 70 71**Widget capability**: This API can be used in ArkTS widgets since API version 9. 72 73**Atomic service API**: This API can be used in atomic services since API version 11. 74 75**System capability**: SystemCapability.ArkUI.ArkUI.Full 76 77**Parameters** 78 79| Name | Type | Mandatory | Description | 80| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 81| value | {overflow: [TextOverflow](ts-appendix-enums.md#textoverflow)} | Yes | Display mode when the text is too long.<br>Default value: **{overflow: TextOverflow.Clip}** | 82 83### maxLines 84 85maxLines(value: number) 86 87Sets the maximum number of lines in the text. By default, text is automatically folded. If this attribute is specified, the text will not exceed the specified number of lines. If there is extra text, you can use [textOverflow](#textoverflow) to specify how it is displayed. 88 89**Widget capability**: This API can be used in ArkTS widgets since API version 9. 90 91**Atomic service API**: This API can be used in atomic services since API version 11. 92 93**System capability**: SystemCapability.ArkUI.ArkUI.Full 94 95**Parameters** 96 97| Name | Type | Mandatory | Description | 98| ------ | ------ | ---- | ---------------- | 99| value | number | Yes | Maximum number of lines in the text. | 100 101### lineHeight 102 103lineHeight(value: number | string | Resource) 104 105Sets the text line height. If the value is less than or equal to **0**, the line height is not limited and the font size is adaptive. If the value is of the number type, the unit fp is used. 106 107**Widget capability**: This API can be used in ArkTS widgets since API version 9. 108 109**Atomic service API**: This API can be used in atomic services since API version 11. 110 111**System capability**: SystemCapability.ArkUI.ArkUI.Full 112 113**Parameters** 114 115| Name | Type | Mandatory | Description | 116| ------ | ------------------------------------------------------------ | ---- | ---------------- | 117| value | number \| string \| [Resource](ts-types.md#resource) | Yes | Text line height. | 118 119### decoration 120 121decoration(value: DecorationStyleInterface) 122 123Sets the color, type, and style of the text decorative line. 124 125**Widget capability**: This API can be used in ArkTS widgets since API version 9. 126 127**Atomic service API**: This API can be used in atomic services since API version 11. 128 129**System capability**: SystemCapability.ArkUI.ArkUI.Full 130 131**Parameters** 132 133| Name | Type | Mandatory | Description | 134| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 135| value | [DecorationStyleInterface<sup>12+</sup>](ts-universal-styled-string.md#decorationstyleinterface) | Yes | Style of the text decorative line.<br>Default value:<br>{<br> type: TextDecorationType.None,<br> color: Color.Black,<br> style: TextDecorationStyle.SOLID <br>}<br>**NOTE**<br>The **style** parameter cannot be used in widgets. | 136 137### baselineOffset 138 139baselineOffset(value: number | string) 140 141Sets the offset of the text baseline. If the value specified is a percentage, the default value is used. 142 143**Widget capability**: This API can be used in ArkTS widgets since API version 9. 144 145**Atomic service API**: This API can be used in atomic services since API version 11. 146 147**System capability**: SystemCapability.ArkUI.ArkUI.Full 148 149**Parameters** 150 151| Name | Type | Mandatory | Description | 152| ------ | -------------------------- | ---- | -------------------------------- | 153| value | number \| string | Yes | Offset of the text baseline.<br>Default value: **0** | 154 155### letterSpacing 156 157letterSpacing(value: number | string) 158 159Sets the letter spacing for a text style. If the value specified is a percentage or 0, the default value is used. 160 161If the value specified is a negative value, the text is compressed. A negative value too small may result in the text being compressed to 0 and no content being displayed. 162 163**Widget capability**: This API can be used in ArkTS widgets since API version 9. 164 165**Atomic service API**: This API can be used in atomic services since API version 11. 166 167**System capability**: SystemCapability.ArkUI.ArkUI.Full 168 169**Parameters** 170 171| Name | Type | Mandatory | Description | 172| ------ | -------------------------- | ---- | -------------- | 173| value | number \| string | Yes | Letter spacing. | 174 175### minFontSize 176 177minFontSize(value: number | string | Resource) 178 179Sets the minimum font size. 180 181For the setting to take effect, this attribute must be used together with [maxFontSize](#maxfontsize) and [maxlines](#maxlines), or layout constraint settings. In addition, it does not take effect for child components or styled strings. 182 183When the adaptive font size is used, the **fontSize** settings do not take effect. 184 185If the value of **minFontSize** is less than or equal to 0, the adaptive font size does not take effect. 186 187**Widget capability**: This API can be used in ArkTS widgets since API version 9. 188 189**Atomic service API**: This API can be used in atomic services since API version 11. 190 191**System capability**: SystemCapability.ArkUI.ArkUI.Full 192 193**Parameters** 194 195| Name | Type | Mandatory | Description | 196| ------ | ------------------------------------------------------------ | ---- | ------------------ | 197| value | number \| string \| [Resource](ts-types.md#resource) | Yes | Minimum font size. | 198 199### maxFontSize 200 201maxFontSize(value: number | string | Resource) 202 203Sets the maximum font size. 204 205For the setting to take effect, this attribute must be used together with [minFontSize](#minfontsize) and [maxlines](#maxlines), or layout constraint settings. In addition, it does not take effect for child components or styled strings. 206 207When the adaptive font size is used, the **fontSize** settings do not take effect. 208 209**Widget capability**: This API can be used in ArkTS widgets since API version 9. 210 211**Atomic service API**: This API can be used in atomic services since API version 11. 212 213**System capability**: SystemCapability.ArkUI.ArkUI.Full 214 215**Parameters** 216 217| Name | Type | Mandatory | Description | 218| ------ | ------------------------------------------------------------ | ---- | ------------------ | 219| value | number \| string \| [Resource](ts-types.md#resource) | Yes | Maximum font size. | 220 221### textCase 222 223textCase(value: TextCase) 224 225Sets the text case. 226 227**Widget capability**: This API can be used in ArkTS widgets since API version 9. 228 229**Atomic service API**: This API can be used in atomic services since API version 11. 230 231**System capability**: SystemCapability.ArkUI.ArkUI.Full 232 233**Parameters** 234 235| Name | Type | Mandatory | Description | 236| ------ | ----------------------------------------- | ---- | ----------------------------------------- | 237| value | [TextCase](ts-appendix-enums.md#textcase) | Yes | Text case.<br>Default value: **TextCase.Normal** | 238 239### copyOption<sup>9+</sup> 240 241copyOption(value: CopyOptions) 242 243Sets whether copy and paste is allowed. If this attribute is set to **CopyOptions.InApp** or **CopyOptions.LocalDevice**, a long press on the text will display a context menu that offers the copy and select-all options. 244 245Because widgets do not have the long press event, the context menu will not be displayed when users long press text. 246 247**Widget capability**: This API can be used in ArkTS widgets since API version 9. 248 249**Atomic service API**: This API can be used in atomic services since API version 11. 250 251**System capability**: SystemCapability.ArkUI.ArkUI.Full 252 253**Parameters** 254 255| Name | Type | Mandatory | Description | 256| ------ | ------------------------------------------------ | ---- | ---------------------------------------------------------- | 257| value | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Yes | Whether copy and paste is allowed.<br>Default value: **CopyOptions.None** | 258 259### draggable<sup>9+</sup> 260 261draggable(value: boolean) 262 263Sets the drag effect of the selected text. 264 265This attribute cannot be used together with the [onDragStart](ts-universal-events-drag-drop.md) event. 266 267It must be used together with [CopyOptions](ts-appendix-enums.md#copyoptions9). When it is set to **true** and **copyOptions** is set to **CopyOptions.InApp** or **CopyOptions.LocalDevice**, the selected text can be dragged and copied to the text box. 268 269**Atomic service API**: This API can be used in atomic services since API version 11. 270 271**System capability**: SystemCapability.ArkUI.ArkUI.Full 272 273**Parameters** 274 275| Name | Type | Mandatory | Description | 276| ------ | ------- | ---- | ------------------------------------ | 277| value | boolean | Yes | Drag effect of the selected text.<br>Default value: **false** | 278 279### font<sup>10+</sup> 280 281font(value: Font) 282 283Sets the text style, covering the font size, font width, font family, and font style. 284 285**Atomic service API**: This API can be used in atomic services since API version 11. 286 287**System capability**: SystemCapability.ArkUI.ArkUI.Full 288 289**Parameters** 290 291| Name | Type | Mandatory | Description | 292| ------ | ------- | ---- | ---------- | 293| value | [Font](ts-types.md#font) | Yes | Text style. | 294 295### textShadow<sup>10+</sup> 296 297textShadow(value: ShadowOptions | Array<ShadowOptions>) 298 299Sets the text shadow. 300 301This API does not work with the **fill** attribute or coloring strategy. 302 303Since API version 11, this API supports input parameters in an array to implement multiple text shadows. 304 305**Widget capability**: This API can be used in ArkTS widgets since API version 10. 306 307**Atomic service API**: This API can be used in atomic services since API version 11. 308 309**System capability**: SystemCapability.ArkUI.ArkUI.Full 310 311**Parameters** 312 313| Name | Type | Mandatory | Description | 314| ------ | ------------------------------------------------------------ | ---- | -------------- | 315| value | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) \| Array<[ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions)><sup>11+</sup> | Yes | Text shadow. | 316 317### heightAdaptivePolicy<sup>10+</sup> 318 319heightAdaptivePolicy(value: TextHeightAdaptivePolicy) 320 321Sets how the adaptive height is determined for the text. 322 323When this attribute is set to **TextHeightAdaptivePolicy.MAX_LINES_FIRST**, the [maxlines](#maxlines) attribute takes precedence for adjusting the text height. If the **maxLines** setting results in a layout beyond the layout constraints, the text will shrink to a font size between [minFontSize](#minfontsize) and [maxFontSize](#maxfontsize) to allow for more content to be shown. 324 325If this attribute is set to **TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST**, the **minFontSize** attribute takes precedence for adjusting the text height. If the text can fit in one line with the **minFontSize** setting, the text will enlarge to the largest possible font size between **minFontSize** and **maxFontSize**. 326 327If this attribute is set to **TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST**, the layout constraints take precedence for adjusting the text height. If the resultant layout is beyond the layout constraints, the text will shrink to a font size between **minFontSize** and **maxFontSize** to respect the layout constraints. If the text still extends beyond the layout constraints after shrinking to **minFontSize**, the lines that exceed the constraints are deleted. 328 329**Atomic service API**: This API can be used in atomic services since API version 11. 330 331**System capability**: SystemCapability.ArkUI.ArkUI.Full 332 333**Parameters** 334 335| Name | Type | Mandatory | Description | 336| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 337| value | [TextHeightAdaptivePolicy](ts-appendix-enums.md#textheightadaptivepolicy10) | Yes | How the adaptive height is determined for the text.<br>Default value: **TextHeightAdaptivePolicy.MAX_LINES_FIRST** | 338 339### textIndent<sup>10+</sup> 340 341textIndent(value: Length) 342 343Sets the indent of the first line text. 344 345**Atomic service API**: This API can be used in atomic services since API version 11. 346 347**System capability**: SystemCapability.ArkUI.ArkUI.Full 348 349**Parameters** 350 351| Name | Type | Mandatory | Description | 352| ------ | ---------------------------- | ---- | ---------------------------- | 353| value | [Length](ts-types.md#length) | Yes | Indent of the first line text.<br>Default value: **0** | 354 355### wordBreak<sup>11+</sup> 356 357wordBreak(value: WordBreak) 358 359Sets the word break rule. 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 (...). 360 361**Atomic service API**: This API can be used in atomic services since API version 11. 362 363**System capability**: SystemCapability.ArkUI.ArkUI.Full 364 365**Parameters** 366 367| Name | Type | Mandatory | Description | 368| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 369| value | [WordBreak](ts-appendix-enums.md#wordbreak11) | Yes | Word break rule.<br>Default value: **WordBreak.BREAK_WORD** | 370 371### selection<sup>11+</sup> 372 373selection(selectionStart: number, selectionEnd: number) 374 375Sets text selection. The selected text is highlighted, and a selection handle is displayed together with a menu of available actions. 376 377When **copyOption** is set to **CopyOptions.None**, the **selection** attribute is not effective. 378 379When **overflow** is set to **TextOverflow.MARQUEE**, the **selection** attribute is not effective. 380 381If the value of **selectionStart** is greater than or equal to that of **selectionEnd**, no text will be selected. The value range is [0, textSize], where **textSize** indicates the maximum number of characters in the text content. If the value is less than 0, the value **0** will be used. If the value is greater than **textSize**, **textSize** will be used. 382 383If the value of **selectionStart** or **selectionEnd** falls within the invisible area, no text will be selected. If clipping is disabled, the text selection outside of the parent component takes effect. 384 385**Atomic service API**: This API can be used in atomic services since API version 11. 386 387**System capability**: SystemCapability.ArkUI.ArkUI.Full 388 389**Parameters** 390 391| Name | Type | Mandatory | Description | 392| -------------- | ------ | ---- | ------------------------------------ | 393| selectionStart | number | Yes | Start position of the selected text.<br>Default value: **-1** | 394| selectionEnd | number | Yes | End position of the selected text.<br>Default value: **-1** | 395 396### ellipsisMode<sup>11+</sup> 397 398ellipsisMode(value: EllipsisMode) 399 400Sets the ellipsis position. For the settings to work, **overflow** must be set to **TextOverflow.Ellipsis** and **maxLines** must be specified. Setting **ellipsisMode** alone does not take effect. 401 402**EllipsisMode.START** and **EllipsisMode.CENTER** take effect only when text overflows in a single line. 403 404**Atomic service API**: This API can be used in atomic services since API version 12. 405 406**System capability**: SystemCapability.ArkUI.ArkUI.Full 407 408**Parameters** 409 410| Name | Type | Mandatory | Description | 411| ------ | --------------------------------------------------- | ---- | ----------------------------------------- | 412| value | [EllipsisMode](ts-appendix-enums.md#ellipsismode11) | Yes | Ellipsis position.<br>Default value: **EllipsisMode.END** | 413 414### enableDataDetector<sup>11+</sup> 415 416enableDataDetector(enable: boolean) 417 418Sets whether to enable text recognition. The recognized entity is in the following style settings: 419 420fontColor: Color.Blue<br>decoration: {<br>type: TextDecorationType.Underline,<br>color: Color.Blue,<br>style: TextDecorationStyle.SOLID<br>} 421 422This API only works on devices that provide text recognition. 423 424When **enableDataDetector** is set to **true** and **dataDetectorConfig** is not set, all types of entities are recognized. 425 426When **copyOption** is set to **CopyOptions.None**, the menu displayed after an entity is clicked does not provide text selection. 427 428**Atomic service API**: This API can be used in atomic services since API version 12. 429 430**System capability**: SystemCapability.ArkUI.ArkUI.Full 431 432**Parameters** 433 434| Name | Type | Mandatory | Description | 435| ------ | ------- | ---- | --------------------------------- | 436| enable | boolean | Yes | Whether to enable text recognition.<br>Default value: **false** | 437 438### dataDetectorConfig<sup>11+</sup> 439 440dataDetectorConfig(config: TextDataDetectorConfig) 441 442Configures text recognition settings. 443 444This API must be used together with **enableDataDetector**. It takes effect only when **enableDataDetector** is set to **true**. 445 446When entities A and B overlap, the following rules are followed: 447 4481. If A ⊂ B, retain B. Otherwise, retain A. 449 4502. When A ⊄ B and B ⊄ A: If A.start < B.start, retain A; otherwise, retain B. 451 452**Atomic service API**: This API can be used in atomic services since API version 12. 453 454**System capability**: SystemCapability.ArkUI.ArkUI.Full 455 456**Parameters** 457 458| Name | Type | Mandatory | Description | 459| ------ | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 460| config | [TextDataDetectorConfig](#textdatadetectorconfig11) | Yes | Text recognition configuration.<br>Default value: {<br>types: [ ],<br>onDetectResultUpdate: null<br>} | 461 462### bindSelectionMenu<sup>11+</sup> 463 464bindSelectionMenu(spanType: TextSpanType, content: CustomBuilder, responseType: TextResponseType, 465 options?: SelectionMenuOptions) 466 467Sets the custom context menu on text selection. 468 469The duration required for a long-press gesture is 600 ms for **bindSelectionMenu** and 800 ms for **bindContextMenu**. When both **bindSelectionMenu** and **bindContextMenu** are set and both are configured to be triggered by a long-press gesture, **bindSelectionMenu** is triggered first. 470 471If the custom menu is too long, embed a [Scroll](./ts-container-scroll.md) component to prevent the keyboard from being blocked. 472 473**Atomic service API**: This API can be used in atomic services since API version 12. 474 475**System capability**: SystemCapability.ArkUI.ArkUI.Full 476 477**Parameters** 478 479| Name | Type | Mandatory | Description | 480| ------------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 481| spantype | [TextSpanType](ts-appendix-enums.md#textspantype11) | Yes | Span type of the menu.<br>Default value: **TextSpanType.TEXT** | 482| content | [CustomBuilder](ts-types.md#custombuilder8) | Yes | Content of the menu. | 483| responseType | [TextResponseType](ts-appendix-enums.md#textresponsetype11) | Yes | Response type of the menu.<br>Default value: **TextResponseType.LONG_PRESS** | 484| options | [SelectionMenuOptions](ts-appendix-enums.md#selectionmenuoptions11) | No | Options of the menu. | 485 486### fontFeature<sup>12+</sup> 487 488fontFeature(value: string) 489 490Sets the font feature, for example, monospaced digits. 491 492Format: normal \| \<feature-tag-value\> 493 494Format of **\<feature-tag-value\>**: \<string\> \[ \<integer\> \| on \| off ] 495 496There can be multiple **\<feature-tag-value\>** values, which are separated by commas (,). 497 498For example, the input format for monospaced clock fonts is "ss01" on. 499 500**System capability**: SystemCapability.ArkUI.ArkUI.Full 501 502**Parameters** 503 504| Name | Type | Mandatory | Description | 505| ------ | ------ | ---- | -------------- | 506| value | string | Yes | Font feature. | 507 508Font feature list<br> 509 510 511Font features are advanced typographic features, such as ligatures and monospace, for OpenType fonts. They are typically used in custom fonts and require the support of the font itself. 512For more information about the font features, see [Low-level font feature settings control: the font-feature-settings property](https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop) and [The Complete CSS Demo for OpenType Features](https://sparanoid.com/lab/opentype-features/). 513 514> **NOTE**<br/> 515> 516> The **Text** component cannot contain both text and the child component **Span** or **ImageSpan**. If both of them exist, only the content in **Span** or **ImageSpan** is displayed. 517> 518> The typesetting engine rounds down the value of [width](ts-universal-attributes-size.md#width) to ensure that the value is an integer. If the typesetting engine rounds up the value instead, the right side of the text may be clipped. 519> 520> When multiple **Text** components are placed in the [Row](ts-container-row.md) container with no specific layout or space allocation settings configured, the components are laid out based on the maximum size of the container. To make sure the sum of the components' main axis sizes does not exceed the main axis size of the container, you can set [layoutWeight](ts-universal-attributes-size.md#layoutweight) or use the [flex layout](ts-universal-attributes-flex-layout.md). 521 522### lineSpacing<sup>12+</sup> 523 524lineSpacing(value: LengthMetrics) 525 526Sets the line spacing of the text. If the value specified is less than or equal to 0, the default value **0** is used. 527 528**System capability**: SystemCapability.ArkUI.ArkUI.Full 529 530**Parameters** 531 532| Name | Type | Mandatory | Description | 533| ------ | ------------------------------------------------------------ | ---- | ---------------- | 534| value | [LengthMetrics](ts-types.md#lengthmetrics12) | Yes | Line spacing. Default value: **0** | 535 536### privacySensitive<sup>12+</sup> 537 538privacySensitive(supported: boolean) 539 540Sets whether to enable privacy mode on widgets. 541 542**Widget capability**: This API can be used in ArkTS widgets since API version 12. 543 544**System capability**: SystemCapability.ArkUI.ArkUI.Full 545 546**Parameters** 547 548| Name | Type | Mandatory | Description | 549| --------- | ------- | ---- | ------------------------------------------------------------ | 550| supported | boolean | Yes | Whether to enable privacy mode on widgets.<br>The value **true** means to enable privacy mode, in which case text is obscured as hyphens (-).<br>Default value: **false**<br>**NOTE**<br>If this parameter is set to **null**, privacy mode is disabled.<br>Enabling privacy mode requires [widget framework support](./ts-universal-attributes-obscured.md). | 551 552### lineBreakStrategy<sup>12+</sup> 553 554lineBreakStrategy(strategy: LineBreakStrategy) 555 556Sets the line break rule. This attribute takes effect when **wordBreak** is not set to **breakAll**. Hyphens are not supported. 557 558**Atomic service API**: This API can be used in atomic services since API version 12. 559 560**System capability**: SystemCapability.ArkUI.ArkUI.Full 561 562**Parameters** 563 564| Name | Type | Mandatory | Description | 565| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------- | 566| strategy | [LineBreakStrategy](ts-appendix-enums.md#linebreakstrategy12) | Yes | Line break rule.<br>Default value: **LineBreakStrategy.GREEDY** | 567 568### textSelectable<sup>12+</sup> 569 570textSelectable(value: TextSelectableMode) 571 572Sets whether the text is selectable and focusable. 573 574**Atomic service API**: This API can be used in atomic services since API version 12. 575 576**System capability**: SystemCapability.ArkUI.ArkUI.Full 577 578**Parameters** 579 580| Name | Type | Mandatory | Description | 581| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 582| value | [TextSelectableMode](ts-appendix-enums.md#textselectablemode12) | Yes | Whether the text is selectable and focusable.<br>Default value: **TextSelectableMode.SELECTABLE_UNFOCUSABLE** | 583 584### editMenuOptions<sup>12+</sup> 585 586editMenuOptions(editMenu: EditMenuOptions) 587 588Sets the extended options of the custom context menu on selection, including the text content, icon, and callback. 589 590**Atomic service API**: This API can be used in atomic services since API version 12. 591 592**System capability**: SystemCapability.ArkUI.ArkUI.Full 593 594**Parameters** 595 596| Name | Type | Mandatory | Description | 597| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 598| editMenu | [EditMenuOptions](ts-text-common.md#editmenuoptions) | Yes | Extended options of the custom context menu on selection. | 599 600## TextDataDetectorConfig<sup>11+</sup> 601 602**Atomic service API**: This API can be used in atomic services since API version 12. 603 604| Name | Type | Mandatory | Description | 605| ------ | -------- | ---- | ------------------------------------------- | 606| types | [TextDataDetectorType[]](ts-appendix-enums.md#textdatadetectortype11) | Yes | Entity types for text recognition. Values **null** and **[]** indicate that all types of entities can be recognized. | 607| onDetectResultUpdate | (result: string) => void | No | Callback invoked when text recognition succeeds.<br>- **result**: text recognition result, in JSON format. | 608 609## Events 610 611In addition to the [universal events](ts-universal-events-click.md), the following events are supported. 612 613### onCopy<sup>11+</sup> 614 615onCopy(callback:(value: string) => void) 616 617Called when data is copied to the pasteboard, which is displayed when the text box is long pressed. Currently, only text can be copied. 618 619**Atomic service API**: This API can be used in atomic services since API version 11. 620 621**System capability**: SystemCapability.ArkUI.ArkUI.Full 622 623**Parameters** 624 625| Name | Type | Mandatory | Description | 626| ------ | ------ | ---- | ---------------- | 627| value | string | Yes | Text that is copied. | 628 629### onTextSelectionChange<sup>11+</sup> 630 631onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void) 632 633Called when the text selection position changes. 634 635**Atomic service API**: This API can be used in atomic services since API version 12. 636 637**System capability**: SystemCapability.ArkUI.ArkUI.Full 638 639**Parameters** 640 641| Name | Type | Mandatory | Description | 642| -------------- | ------ | ---- | -------------------- | 643| selectionStart | number | Yes | Start position of the selected text. | 644| selectionEnd | number | Yes | End position of the selected text. | 645 646## TextOptions<sup>11+</sup> 647 648Describes the initialization options of the **Text** component. 649 650| Name | Type | Mandatory | Description | 651| -------- | -------- | -------- | -------- | 652| controller | [TextController](#textcontroller11) | Yes | Text controller. | 653 654## TextController<sup>11+</sup> 655 656Defines the controller of the **Text** component. 657 658**Atomic service API**: This API can be used in atomic services since API version 12. 659 660### Objects to Import 661 662``` 663controller: TextController = new TextController() 664``` 665 666### closeSelectionMenu 667 668closeSelectionMenu(): void 669 670Closes the custom or default context menu on selection. 671 672**Atomic service API**: This API can be used in atomic services since API version 12. 673 674### setStyledString<sup>12+</sup> 675 676setStyledString(value: StyledString): void 677 678Binds to or updates the specified styled string. 679 680**Parameters** 681 682| Name | Type | Mandatory | Description | 683| ----- | ------ | ---- | ------------------- | 684| value | [StyledString](ts-universal-styled-string.md#styledstring) | Yes | Styled string.<br>**NOTE**<br>The child class [MutableStyledString](ts-universal-styled-string.md#mutablestyledstring) of **StyledString** can also serve as the argument. | 685 686### getLayoutManager<sup>12+</sup> 687 688getLayoutManager(): LayoutManager 689 690Obtains the **LayoutManager** object. 691 692**Atomic service API**: This API can be used in atomic services since API version 12. 693 694**Return value** 695 696| Type | Description | 697| ---------------------------------------- | ------- | 698| [LayoutManager](ts-text-common.md#LayoutManager) | **LayoutManager** object. | 699 700## Example 701 702### Example 1 703 704This example shows how to set the **textAlign**, **maxLines**, **textOverflow**, and **lineHeight** attributes. 705 706```ts 707// xxx.ets 708@Extend(Text) 709function style(TextAlign: TextAlign) { 710 .textAlign(TextAlign) 711 .fontSize(12) 712 .border({ width: 1 }) 713 .padding(10) 714 .width('100%') 715} 716 717@Entry 718@Component 719struct TextExample1 { 720 build() { 721 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 722 // Set the horizontal alignment for the text. 723 // Single-line text 724 Text('textAlign').fontSize(9).fontColor(0xCCCCCC) 725 Text('TextAlign set to Center.') 726 .style(TextAlign.Center) 727 Text('TextAlign set to Start.') 728 .style(TextAlign.Start) 729 Text('TextAlign set to End.') 730 .style(TextAlign.End) 731 732 // Multi-line text 733 Text('This is the text content with textAlign set to Center.') 734 .style(TextAlign.Center) 735 Text('This is the text content with textAlign set to Start.') 736 .style(TextAlign.Start) 737 Text('This is the text content with textAlign set to End.') 738 .style(TextAlign.End) 739 740 741 // Set the display mode when the text is too long. 742 Text('TextOverflow+maxLines').fontSize(9).fontColor(0xCCCCCC) 743 // Clip the text when the value of maxLines is exceeded. 744 Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content. This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content.') 745 .textOverflow({ overflow: TextOverflow.Clip }) 746 .maxLines(1) 747 .style(TextAlign.Start) 748 749 // Show an ellipsis (...) when the value of maxLines is exceeded. 750 Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.') 751 .textOverflow({ overflow: TextOverflow.Ellipsis }) 752 .maxLines(1) 753 .style(TextAlign.Start) 754 755 Text('lineHeight').fontSize(9).fontColor(0xCCCCCC) 756 Text('This is the text with the line height set. This is the text with the line height set.') 757 .style(TextAlign.Start) 758 Text('This is the text with the line height set. This is the text with the line height set.') 759 .style(TextAlign.Start) 760 .lineHeight(20) 761 }.height(600).width(340).padding({ left: 35, right: 35, top: 35 }) 762 } 763} 764``` 765 766 767### Example 2 768 769This example shows how to set the **decoration**, **baselineOffset**, **letterSpacing**, and **textCase** attributes. 770 771```ts 772@Extend(Text) 773function style() { 774 .fontSize(12) 775 .border({ width: 1 }) 776 .padding(10) 777 .width('100%') 778} 779 780@Entry 781@Component 782struct TextExample2 { 783 build() { 784 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 785 Text('decoration').fontSize(9).fontColor(0xCCCCCC) 786 Text('This is the text content with the decoration set to LineThrough and the color set to Red.') 787 .decoration({ 788 type: TextDecorationType.LineThrough, 789 color: Color.Red 790 }) 791 .style() 792 793 Text('This is the text content with the decoration set to Overline and the color set to Red.') 794 .decoration({ 795 type: TextDecorationType.Overline, 796 color: Color.Red, 797 style: TextDecorationStyle.DOTTED 798 }) 799 .style() 800 801 Text('This is the text content with the decoration set to Underline and the color set to Red.') 802 .decoration({ 803 type: TextDecorationType.Underline, 804 color: Color.Red, 805 style: TextDecorationStyle.WAVY 806 }) 807 .style() 808 809 // Set the offset of the text baseline. 810 Text('baselineOffset').fontSize(9).fontColor(0xCCCCCC) 811 Text('This is the text content with baselineOffset 0.') 812 .baselineOffset(0) 813 .style() 814 Text('This is the text content with baselineOffset 30.') 815 .baselineOffset(30) 816 .style() 817 Text('This is the text content with baselineOffset -20.') 818 .baselineOffset(-20) 819 .style() 820 821 // Set the letter spacing. 822 Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC) 823 Text('This is the text content with letterSpacing 0.') 824 .letterSpacing(0) 825 .style() 826 Text('This is the text content with letterSpacing 3.') 827 .letterSpacing(3) 828 .style() 829 Text('This is the text content with letterSpacing -1.') 830 .letterSpacing(-1) 831 .style() 832 833 Text('textCase').fontSize(9).fontColor(0xCCCCCC) 834 Text('This is the text content with textCase set to Normal.') 835 .textCase(TextCase.Normal) 836 .style() 837 // Display the text in lowercase. 838 Text('This is the text content with textCase set to LowerCase.') 839 .textCase(TextCase.LowerCase) 840 .style() 841 // Display the text in uppercase. 842 Text('This is the text content with textCase set to UpperCase.') 843 .textCase(TextCase.UpperCase) 844 .style() 845 846 }.height(700).width(350).padding({ left: 35, right: 35, top: 35 }) 847 } 848} 849``` 850 851 852### Example 3 853 854This example shows how to use **textShadow**, **heightAdaptivePolicy**, and **TextOverflow.MARQUEE**. 855 856```ts 857@Extend(Text) 858function style(HeightAdaptivePolicy: TextHeightAdaptivePolicy) { 859 .width('80%') 860 .height(90) 861 .borderWidth(1) 862 .minFontSize(10) 863 .maxFontSize(30) 864 .maxLines(2) 865 .textOverflow({ overflow: TextOverflow.Ellipsis }) 866 .heightAdaptivePolicy(HeightAdaptivePolicy) 867} 868 869@Entry 870@Component 871struct TextExample3 { 872 build() { 873 Column({ space: 8 }) { 874 Text('textShadow').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 875 // Set the text shadow. 876 Text('textShadow') 877 .width('80%') 878 .height(55) 879 .fontSize(40) 880 .lineHeight(55) 881 .textAlign(TextAlign.Center) 882 .textShadow({ 883 radius: 10, 884 color: Color.Black, 885 offsetX: 0, 886 offsetY: 0 887 }) 888 .borderWidth(1) 889 Divider() 890 // Set how the adaptive height is determined for the text. 891 Text('heightAdaptivePolicy').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 892 Text('This is the text with the height adaptive policy set') 893 .style(TextHeightAdaptivePolicy.MAX_LINES_FIRST) 894 Text('This is the text with the height adaptive policy set') 895 .style(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST) 896 Text('This is the text with the height adaptive policy set') 897 .style(TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST) 898 Divider() 899 Text('marquee').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 900 // Set the text to continuously scroll when text overflow occurs. 901 Text('This is the text with the text overflow set marquee') 902 .width(300) 903 .borderWidth(1) 904 .textOverflow({ overflow: TextOverflow.MARQUEE }) 905 } 906 } 907} 908``` 909 910 911 912### Example 4 913This example shows how to use **ellipsisMode** and **wordBreak**. 914 915```ts 916// xxx.ets 917@Entry 918@Component 919struct TextExample4 { 920 @State text: string = 921 'The text component is used to display a piece of textual information.Support universal attributes and universal text attributes.' 922 @State ellipsisModeIndex: number = 0; 923 @State ellipsisMode: EllipsisMode[] = [EllipsisMode.START, EllipsisMode.CENTER, EllipsisMode.END] 924 @State ellipsisModeStr: string[] = ['START', 'CENTER', 'END'] 925 @State wordBreakIndex: number = 0; 926 @State wordBreak: WordBreak[] = [WordBreak.NORMAL, WordBreak.BREAK_ALL, WordBreak.BREAK_WORD] 927 @State wordBreakStr: string[] = ['NORMAL', 'BREAK_ALL', 'BREAK_WORD'] 928 @State textClip: boolean = false 929 930 build() { 931 Column({ space: 10 }) { 932 Text(this.text) 933 .fontSize(16) 934 .border({ width: 1 }) 935 .lineHeight(20) 936 .maxLines(1) 937 .textOverflow({ overflow: TextOverflow.Ellipsis }) 938 .ellipsisMode(this.ellipsisMode[this.ellipsisModeIndex]) 939 .width(300) 940 .margin({ left: 20, top: 20 }) 941 942 Row() { 943 Button('Change Ellipsis Position:' + this.ellipsisModeStr[this.ellipsisModeIndex]).onClick (() => { 944 this.ellipsisModeIndex++ 945 if (this.ellipsisModeIndex > (this.ellipsisModeStr.length - 1)) { 946 this.ellipsisModeIndex = 0 947 } 948 }) 949 } 950 951 Text('This is set wordBreak to WordBreak text Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu.') 952 .fontSize(12) 953 .border({ width: 1 }) 954 .wordBreak(WordBreak.NORMAL) 955 .lineHeight(20) 956 .maxLines(2) 957 .clip(this.textClip) 958 .width(260) 959 Row() { 960 Button('Change Clip Mode: ' + this.textClip).onClick(() => { 961 this.textClip = !this.textClip 962 }) 963 } 964 965 Text(this.text) 966 .fontSize(12) 967 .border({ width: 1 }) 968 .maxLines(2) 969 .textOverflow({ overflow: TextOverflow.Ellipsis }) 970 .wordBreak(this.wordBreak[this.wordBreakIndex]) 971 .lineHeight(20) 972 .width(260) 973 Row() { 974 Button('Change wordBreak Mode: ' + this.wordBreakStr[this.wordBreakIndex]).onClick(() => { 975 this.wordBreakIndex++ 976 if (this.wordBreakIndex > (this.wordBreakStr.length - 1)) { 977 this.wordBreakIndex = 0 978 } 979 }) 980 } 981 } 982 } 983} 984``` 985 986 987 988### Example 5 989This example shows how to use **selection** and **onCopy**. 990 991```ts 992@Entry 993@Component 994struct TextExample5 { 995 @State onCopy: string = '' 996 @State text: string = 'This is set selection to Selection text content This is set selection to Selection text content.' 997 @State start: number = 0 998 @State end: number = 20 999 1000 build() { 1001 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { 1002 Text(this.text) 1003 .fontSize(12) 1004 .border({ width: 1 }) 1005 .lineHeight(20) 1006 .margin(30) 1007 .copyOption(CopyOptions.InApp) 1008 .selection(this.start, this.end) 1009 .onCopy((value: string) => { 1010 this.onCopy = value 1011 }) 1012 Button('Set text selection') 1013 .margin({left:20}) 1014 .onClick(() => { 1015 // Change the start point and end point of the text selection. 1016 this.start = 10 1017 this.end = 30 1018 }) 1019 Text(this.onCopy).fontSize(12).margin(10).key('copy') 1020 }.height(600).width(335).padding({ left: 35, right: 35, top: 35 }) 1021 } 1022} 1023``` 1024 1025 1026### Example 6 1027This example shows how to use **enableDataDetector** and **dataDetectorConfig**. 1028 1029```ts 1030@Entry 1031@Component 1032struct TextExample6 { 1033 @State phoneNumber: string = '(86) (755) ********'; 1034 @State url: string = 'www.********.com'; 1035 @State email: string = '***@example.com'; 1036 @State address: string = 'XX (province) XX (city) XX (county) XXXX'; 1037 @State datetime: string = 'XX-XX-XX XXXX'; 1038 @State enableDataDetector: boolean = true; 1039 @State types: TextDataDetectorType[] = []; 1040 1041 build() { 1042 Row() { 1043 Column() { 1044 Text( 1045 'Phone number:' + this.phoneNumber + '\n' + 1046 'URL:' + this.url + '\n' + 1047 'Email:' + this.email + '\n' + 1048 'Address:' + this.address + '\n' + 1049 'Time:' + this.datetime 1050 ) 1051 .fontSize(16) 1052 .copyOption(CopyOptions.InApp) 1053 .enableDataDetector(this.enableDataDetector) 1054 .dataDetectorConfig({types : this.types, onDetectResultUpdate: (result: string)=>{}}) 1055 .textAlign(TextAlign.Center) 1056 .borderWidth(1) 1057 .padding(10) 1058 .width('100%') 1059 } 1060 .width('100%') 1061 } 1062 .height('100%') 1063 } 1064} 1065``` 1066 1067 1068 1069### Example 7 1070 1071This example shows how to use **bindSelectionMenu**, **onTextSelectionChange**, and **closeSelectionMenu**. 1072 1073```ts 1074@Entry 1075@Component 1076struct TextExample7 { 1077 controller: TextController = new TextController(); 1078 options: TextOptions = { controller: this.controller }; 1079 1080 build() { 1081 Column() { 1082 Column() { 1083 Text(undefined, this.options) { 1084 Span('Hello World') 1085 ImageSpan($r('app.media.icon')) 1086 .width('100px') 1087 .height('100px') 1088 .objectFit(ImageFit.Fill) 1089 .verticalAlign(ImageSpanAlignment.CENTER) 1090 } 1091 .copyOption(CopyOptions.InApp) 1092 .bindSelectionMenu(TextSpanType.IMAGE, this.LongPressImageCustomMenu, TextResponseType.LONG_PRESS, { 1093 onDisappear: () => { 1094 console.info(`Triggered when the custom context menu on selection is closed.`); 1095 }, 1096 onAppear: () => { 1097 console.info(`Triggered when the custom context menu on selection is displayed`); 1098 } 1099 }) 1100 .bindSelectionMenu(TextSpanType.TEXT, this.RightClickTextCustomMenu, TextResponseType.RIGHT_CLICK) 1101 .bindSelectionMenu(TextSpanType.MIXED, this.SelectMixCustomMenu, TextResponseType.SELECT) 1102 .onTextSelectionChange((selectionStart: number, selectionEnd: number) => { 1103 console.info(`Triggered when the text selection position changes, selectionStart: ${selectionStart}, selectionEnd: ${selectionEnd}`); 1104 }) 1105 .borderWidth(1) 1106 .borderColor(Color.Red) 1107 .width(200) 1108 .height(100) 1109 } 1110 .width('100%') 1111 .backgroundColor(Color.White) 1112 .alignItems(HorizontalAlign.Start) 1113 .padding(25) 1114 } 1115 .height('100%') 1116 } 1117 1118 @Builder 1119 RightClickTextCustomMenu() { 1120 Column() { 1121 Menu() { 1122 MenuItemGroup() { 1123 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Right Click Menu 1", labelInfo: "" }) 1124 .onClick((event) => { 1125 this.controller.closeSelectionMenu(); 1126 }) 1127 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Right Click Menu 2", labelInfo: "" }) 1128 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Right Click Menu 3", labelInfo: "" }) 1129 } 1130 } 1131 .MenuStyles() 1132 } 1133 } 1134 1135 @Builder 1136 LongPressImageCustomMenu() { 1137 Column() { 1138 Menu() { 1139 MenuItemGroup() { 1140 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Long Press Image Menu 1", labelInfo: "" }) 1141 .onClick((event) => { 1142 this.controller.closeSelectionMenu(); 1143 }) 1144 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Long Press Image Menu 2", labelInfo: "" }) 1145 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Long Press Image Menu 3", labelInfo: "" }) 1146 } 1147 } 1148 .MenuStyles() 1149 } 1150 } 1151 1152 @Builder 1153 SelectMixCustomMenu() { 1154 Column() { 1155 Menu() { 1156 MenuItemGroup() { 1157 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 1", labelInfo: "" }) 1158 .onClick((event) => { 1159 this.controller.closeSelectionMenu(); 1160 }) 1161 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 2", labelInfo: "" }) 1162 MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 3", labelInfo: "" }) 1163 } 1164 } 1165 .MenuStyles() 1166 } 1167 } 1168} 1169 1170@Extend(Menu) 1171function MenuStyles() { 1172 .radius($r('sys.float.ohos_id_corner_radius_card')) 1173 .clip(true) 1174 .backgroundColor('#F0F0F0') 1175} 1176``` 1177 1178 1179 1180### Example 8 1181This example shows how to use **fontFeature**, **lineSpacing**, and **lineBreakStrategy**. 1182 1183```ts 1184import { LengthMetrics } from '@kit.ArkUI' 1185 1186@Extend(Text) function lineSpacingValue(LineSpacing: LengthMetrics|undefined) { 1187 .lineSpacing(LineSpacing) 1188 .fontSize(12) 1189 .border({ width: 1 }) 1190} 1191@Entry 1192@Component 1193struct TextExample8 { 1194 @State message1: string = "They can be classified as built-in components–those directly provided by the ArkUI framework and custom components – those defined by developers" + 1195 "The built-in components include buttons radio buttonsprogress indicators and text You can set the rendering effectof thesecomponents in method chaining mode," + 1196 "page components are divided into independent UI units to implementindependent creation development and reuse of different units on pages making pages more engineering-oriented."; 1197 @State lineBreakStrategyIndex: number = 0; 1198 @State lineBreakStrategy: LineBreakStrategy[] = [LineBreakStrategy.GREEDY, LineBreakStrategy.HIGH_QUALITY, LineBreakStrategy.BALANCED] 1199 @State lineBreakStrategyStr: string[] = ['GREEDY', 'HIGH_QUALITY', 'BALANCED'] 1200 build() { 1201 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 1202 Text('lineSpacing').fontSize(9).fontColor(0xCCCCCC) 1203 Text('This is a context with no lineSpacing set.') 1204 .lineSpacingValue(undefined) 1205 Text( 'This is a context with lineSpacing set to 20_px.') 1206 .lineSpacingValue(LengthMetrics.px(20)) 1207 Text('This is the context with lineSpacing set to 20_vp.') 1208 .lineSpacingValue(LengthMetrics.vp(20)) 1209 Text('This is the context with lineSpacing set to 20_fp.') 1210 .lineSpacingValue(LengthMetrics.fp(20)) 1211 Text('This is the context with lineSpacing set to 20_lpx.') 1212 .lineSpacingValue(LengthMetrics.lpx(20)) 1213 Text('This is the context with lineSpacing set to 100%.') 1214 .lineSpacingValue(LengthMetrics.percent(1)) 1215 Text('fontFeature').fontSize(9).fontColor(0xCCCCCC) 1216 Text('This is ss01 on : 0123456789') 1217 .fontSize(20) 1218 .fontFeature("\"ss01\" on") 1219 Text('This is ss01 off: 0123456789') 1220 .fontSize(20) 1221 .fontFeature("\"ss01\" off") 1222 Text('lineBreakStrategy').fontSize(9).fontColor(0xCCCCCC) 1223 Text(this.message1) 1224 .fontSize(12) 1225 .border({ width: 1 }) 1226 .padding(10) 1227 .width('100%') 1228 .lineBreakStrategy(this.lineBreakStrategy[this.lineBreakStrategyIndex]) 1229 Row() { 1230 Button('Change lineBreakStrategy Value:' + this.lineBreakStrategyStr[this.lineBreakStrategyIndex]).onClick(() => { 1231 this.lineBreakStrategyIndex++ 1232 if(this.lineBreakStrategyIndex > (this.lineBreakStrategyStr.length - 1)) { 1233 this.lineBreakStrategyIndex = 0 1234 } 1235 }) 1236 } 1237 }.height(600).width(350).padding({ left: 35, right: 35, top: 35 }) 1238 } 1239} 1240``` 1241 1242 1243 1244### Example 9 1245This example shows how to use **getLayoutManager**. 1246 1247```ts 1248@Entry 1249@Component 1250struct TextExample9 { 1251 @State lineCount: string = "" 1252 @State glyphPositionAtCoordinate: string = "" 1253 @State lineMetrics: string = "" 1254 controller: TextController = new TextController() 1255 @State textStr: string = 1256 'Hello World!' 1257 1258 build() { 1259 Scroll() { 1260 Column() { 1261 Text('Use getLayoutManager to get layout information') 1262 .fontSize(9) 1263 .fontColor(0xCCCCCC) 1264 .width('90%') 1265 .padding(10) 1266 Text(this.textStr, { controller: this.controller }) 1267 .fontSize(25) 1268 .borderWidth(1) 1269 .onAreaChange(() => { 1270 let layoutManager: LayoutManager = this.controller.getLayoutManager() 1271 this.lineCount = "LineCount: " + layoutManager.getLineCount() 1272 }) 1273 1274 Text('LineCount').fontSize(9).fontColor(0xCCCCCC).width('90%').padding(10) 1275 Text(this.lineCount) 1276 1277 Text('GlyphPositionAtCoordinate').fontSize(9).fontColor(0xCCCCCC).width('90%').padding(10) 1278 Button("Relative Component Coordinates [150,50]") 1279 .onClick(() => { 1280 let layoutManager: LayoutManager = this.controller.getLayoutManager() 1281 let position: PositionWithAffinity = layoutManager.getGlyphPositionAtCoordinate(150, 50) 1282 this.glyphPositionAtCoordinate = 1283 "Relative component coordinates [150,50] glyphPositionAtCoordinate position: " + position.position + " affinity: " + 1284 position.affinity 1285 }) 1286 .margin({ bottom: 20, top: 10 }) 1287 Text(this.glyphPositionAtCoordinate) 1288 1289 Text('LineMetrics').fontSize(9).fontColor(0xCCCCCC).width('90%').padding(10) 1290 Button("Line Metrics") 1291 .onClick(() => { 1292 let layoutManager: LayoutManager = this.controller.getLayoutManager() 1293 let lineMetrics: LineMetrics = layoutManager.getLineMetrics(0) 1294 this.lineMetrics = "lineMetrics is " + JSON.stringify(lineMetrics) + '\n\n' 1295 let runMetrics = lineMetrics.runMetrics 1296 runMetrics.forEach((value, key) => { 1297 this.lineMetrics += "runMetrics key is " + key + " " + JSON.stringify(value) + "\n\n" 1298 }); 1299 }) 1300 .margin({ bottom: 20, top: 10 }) 1301 Text(this.lineMetrics) 1302 } 1303 .margin({ top: 100, left: 8, right: 8 }) 1304 } 1305 } 1306} 1307``` 1308 1309 1310 1311### Example 10 1312This example shows how to use **textSelectable**: With **TextSelectMode.SELECTABLE_FOCUSABLE** set, the text can be selected with the keyboard. 1313 1314```ts 1315@Entry 1316@Component 1317struct TextExample10 { 1318 @State message: string = 'TextTextTextTextTextTextTextText' + 'TextTextTextTextTextTextTextTextTextTextTextTextTextTextTextText'; 1319 1320 build() { 1321 Column() { 1322 Text(this.message) 1323 .width(300) 1324 .height(100) 1325 .maxLines(5) 1326 .fontColor(Color.Black) 1327 .copyOption(CopyOptions.InApp) 1328 .selection(3, 8) 1329 .textSelectable(TextSelectableMode.SELECTABLE_FOCUSABLE) 1330 }.width('100%').margin({ top: 100 }) 1331 } 1332} 1333``` 1334 1335 1336 1337### Example 11 1338 1339This example shows how to set **editMenuOptions**. 1340 1341```ts 1342// xxx.ets 1343@Entry 1344@Component 1345struct TextExample11 { 1346 @State text: string = 'Text editMenuOptions' 1347 1348 onCreateMenu(menuItems: Array<TextMenuItem>) { 1349 menuItems.forEach((value, index) => { 1350 value.icon = $r('app.media.startIcon') 1351 if (value.id.equals(TextMenuItemId.COPY)) { 1352 value.content = "Copy_Custom" 1353 } 1354 if (value.id.equals(TextMenuItemId.SELECT_ALL)) { 1355 value.content = "Select All_Custom" 1356 } 1357 }) 1358 let item1: TextMenuItem = { 1359 content: 'Custom 1', 1360 icon: $r('app.media.startIcon'), 1361 id: TextMenuItemId.of('Custom 1'), 1362 } 1363 let item2: TextMenuItem = { 1364 content: 'Custom 2', 1365 id: TextMenuItemId.of('Custom 2'), 1366 icon: $r('app.media.startIcon'), 1367 } 1368 menuItems.push(item1) 1369 menuItems.unshift(item2) 1370 return menuItems 1371 } 1372 1373 build() { 1374 Column() { 1375 Text(this.text) 1376 .fontSize(20) 1377 .copyOption(CopyOptions.LocalDevice) 1378 .editMenuOptions({ 1379 onCreateMenu: this.onCreateMenu, onMenuItemClick: (menuItem: TextMenuItem, textRange: TextRange) => { 1380 if (menuItem.id.equals(TextMenuItemId.of("custom2"))) { 1381 console.log("Intercepted id: custom2 start:" + textRange.start + "; end:" + textRange.end) 1382 return true; 1383 } 1384 if (menuItem.id.equals(TextMenuItemId.COPY)) { 1385 console.log("Intercepted COPY start:" + textRange.start + "; end:" + textRange.end) 1386 return true; 1387 } 1388 if (menuItem.id.equals(TextMenuItemId.SELECT_ALL)) { 1389 console.log("Did not intercept SELECT_ALL start:" + textRange.start + "; end:" + textRange.end) 1390 return false; 1391 } 1392 return false; 1393 } 1394 }) 1395 .margin({ top: 100 }) 1396 } 1397 .width("90%") 1398 .margin("5%") 1399 } 1400} 1401``` 1402 1403 1404