1e41f4b71Sopenharmony_ci# Styled String 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciStyled strings are string objects that facilitate the flexible use of text styles. They can be bound to the **Text** component using the [setStyledString](./ts-basic-components-text.md#setstyledstring12) API in **TextController**, and to the **RichEditor** component using the [setStyledString](ts-basic-components-richeditor.md#setstyledstring12) API in **RichEditorStyledStringController**. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 12. Updates will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> Currently, styled strings cannot be used in worker threads. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Rules of Use 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci* If a styled string conflicts with the current style settings in a component, the style set in the styled string takes effect. 14e41f4b71Sopenharmony_ci* If a styled string conflicts with the child components in [Text](./ts-basic-components-text.md), the style set in the styled string is applied to the **Text** component, and style settings of the child components, including [Span](./ts-basic-components-span.md), are ignored. 15e41f4b71Sopenharmony_ci* @State is not supported. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## StyledString 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciconstructor(value: string | ImageAttachment | CustomSpan , styles?: Array\<StyleOptions>) 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**Parameters** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 26e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 27e41f4b71Sopenharmony_ci| value | string \| [ImageAttachment](#imageattachment) \| [CustomSpan](#customspan) | Yes| Text of the styled string.<br>**NOTE**<br>If this parameter is of the ImageAttachment or CustomSpan type, the **styles** parameter does not take effect. | 28e41f4b71Sopenharmony_ci| styles | Array<[StyleOptions](#styleoptions)> | No| Initialization options of the styled string.<br>**NOTE**<br>If **start** is set to an invalid value, it uses the default value **0**.<br>If the value of **start** is valid but that of **length** is invalid, the difference between the length of the styled string and the value of **start** is used as the value of **length**.<br>If **StyledStringKey** does not match **StyledStringValue**, it does not take effect.<br>The **styledKey** parameter has no default value.| 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci### Attributes 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci| Name | Type | Read Only | Optional | Description | 37e41f4b71Sopenharmony_ci| ------ | ------ | ------ | ------ | -------------- | 38e41f4b71Sopenharmony_ci| length | number | Yes | No | Length of the styled string.<br>**NOTE**<br>If the styled string contains an image or a custom span, its length is 1.| 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci### getString 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_cigetString(): string 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciObtains the text of this styled string. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**Return value** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci| Type |Description | 53e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 54e41f4b71Sopenharmony_ci| string | Text of the styled string.<br>**NOTE**<br>If the styled string contains an image, the image is represented by a space in the return value.| 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci### equals 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciequals(other: StyledString): boolean 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ciChecks whether this styled string the same as another styled string. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**Parameters** 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 69e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 70e41f4b71Sopenharmony_ci| other | [StyledString](#styledstring) | Yes | **StyledString** object to compare.| 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci**Return value** 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci| Type | Description | 75e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 76e41f4b71Sopenharmony_ci| boolean | Whether two styled strings are equal.<br>**NOTE**<br>The two styled strings are the same if they have the same text and style.<br>**GestureStyle** in styled strings is not compared. This means that, if two styled strings are the same except for the event configured, they are the same.<br>In comparing **CustomSpan** objects, addresses are compared. The objects that have the same address are the same.| 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci### subStyledString 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ciObtains a substring of this styled string. 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_cisubStyledString(start: number , length?: number): StyledString 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci**Parameters** 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 91e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 92e41f4b71Sopenharmony_ci| start | number | Yes | Subscript that corresponds to the start position of the styled substring.| 93e41f4b71Sopenharmony_ci| length | number | No | Length of the styled substring.| 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**Return value** 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci| Type | Description | 98e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 99e41f4b71Sopenharmony_ci| [StyledString](#styledstring) | Styled substring.<br>**NOTE**<br>If the value of **start** is valid, the difference between the length of the styled string and the value of **start** is used as the default value of **length**.<br>If the values of **start** and **length** are out of the acceptable range or if any mandatory parameter is passed as **undefined**, an exception is thrown.| 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci**Error codes** 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| ID| Error Message| 106e41f4b71Sopenharmony_ci| ------- | -------- | 107e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci### getStyles 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ciObtains the styles in the specified range of a styled string. 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_cigetStyles(start: number , length: number , styledKey?: StyledStringKey): Array\<SpanStyle> 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**Parameters** 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 122e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 123e41f4b71Sopenharmony_ci| start | number | Yes | Subscript that corresponds to the target range in the styled string.| 124e41f4b71Sopenharmony_ci| length | number | Yes | Length of the target range in the styled string.| 125e41f4b71Sopenharmony_ci| styledKey | [StyledStringKey](#styledstringkey12) | No | Style key of the styled string.| 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**Return value** 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci| Type | Description | 130e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 131e41f4b71Sopenharmony_ci| Array<[SpanStyle](#spanstyle)> | Array of styles.<br>**NOTE**<br>If no style is set for the specified range in the styled string, an empty array is returned.<br>If the values of **start** and **length** are out of the acceptable range or if any mandatory parameter is passed as **undefined**, an exception is thrown.<br>If **styledKey** is set to an invalid value or **undefined**, an exception is thrown.<br>If **styledKey** is a **CustomSpan** object, the style returned is the one passed to create the object. That is, modifying the style object also affects the actual display effect.| 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**Error codes** 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci| ID| Error Message| 138e41f4b71Sopenharmony_ci| ------- | -------- | 139e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci### fromHtml 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ciConverts an HTML-formatted string into a styled string. Currently, the conversion supports the following HTML tags: **\<p>**, **\<span>**, **\<img>**. Only the styles defined by the **style** attributes of these tags can be converted into corresponding styles in styled strings. 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_cistatic fromHtml(html: string): Promise\<StyledString> 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci**Parameters** 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 154e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 155e41f4b71Sopenharmony_ci| html | string | Yes | HTML-formatted string.| 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**Return value** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci| Type | Description | 160e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 161e41f4b71Sopenharmony_ci| [StyledString](#styledstring) | Styled string.| 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci**Error codes** 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci| ID| Error Message| 168e41f4b71Sopenharmony_ci| ------- | -------- | 169e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci## MutableStyledString 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ciInherits from the [StyledString](#styledstring) class. 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci> **An exception is thrown in the following cases:** 176e41f4b71Sopenharmony_ci> 177e41f4b71Sopenharmony_ci> The values of **start** and **length** are out of the acceptable range or if any mandatory parameter is passed as **undefined**. 178e41f4b71Sopenharmony_ci> 179e41f4b71Sopenharmony_ci> **styledKey** or **styledValue** is set to an invalid value or they do not match. 180e41f4b71Sopenharmony_ci> 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci### replaceString 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ciReplaces the string in the specified range of this styled string. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_cireplaceString(start: number , length: number , other: string): void 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci**Parameters** 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 195e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 196e41f4b71Sopenharmony_ci| start | number | Yes | Subscript of the target range.| 197e41f4b71Sopenharmony_ci| length | number | Yes | Length of the target range.| 198e41f4b71Sopenharmony_ci| other | string | Yes | String to replace the content in the target range.<br>**NOTE**<br>The string specified here uses the style of the character at the **start** position.| 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci**Error codes** 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci| ID| Error Message| 205e41f4b71Sopenharmony_ci| ------- | -------- | 206e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci### insertString 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ciInserts a string. 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ciinsertString(start: number , other: string): void 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci**Parameters** 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 221e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 222e41f4b71Sopenharmony_ci| start | number | Yes | Subscript of the position where the string will be inserted.| 223e41f4b71Sopenharmony_ci| other | string | Yes | String to insert.<br>**NOTE**<br>The string specified here uses the style of the character at the **start** - 1 position or, if that character does not have style set, the style of the character at the **start** position.| 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**Error codes** 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci| ID| Error Message| 230e41f4b71Sopenharmony_ci| ------- | -------- | 231e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci### removeString 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ciRemoves the string in the specified range of this styled string. 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ciThis API equally works when the styled string contains an image. 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ciremoveString(start: number , length: number): void 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci**Parameters** 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 248e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 249e41f4b71Sopenharmony_ci| start | number | Yes | Subscript of the target range.| 250e41f4b71Sopenharmony_ci| length | number | Yes | Length of the target range.| 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci**Error codes** 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci| ID| Error Message| 257e41f4b71Sopenharmony_ci| ------- | -------- | 258e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci### replaceStyle 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ciReplaces the style in the specified range of this styled string. 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_cireplaceStyle(spanStyle: SpanStyle): void 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci**Parameters** 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 273e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 274e41f4b71Sopenharmony_ci| spanStyle | [SpanStyle](#spanstyle) | Yes | Style object.<br>**NOTE**<br>By default, the original style is removed and replaced with the new style.<br>If **styledKey** of **SpanStyle** is **IMAGE**, this API takes effect only when an image with the length of 1 is at the **start** position.| 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci**Error codes** 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci| ID| Error Message| 281e41f4b71Sopenharmony_ci| ------- | -------- | 282e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci### setStyle 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ciSets a new style for the specified range of this styled string. 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_cisetStyle(spanStyle: SpanStyle): void 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci**Parameters** 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 297e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 298e41f4b71Sopenharmony_ci| spanStyle | [SpanStyle](#spanstyle) | Yes | Style object.<br>**NOTE**<br>By default, the new style is applied without removing the original style. If the style already exists, it is updated.<br>If **styledKey** of **SpanStyle** is **IMAGE**, this API takes effect only when an image with the length of 1 is at the **start** position.| 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci### removeStyle 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ciRemoves the style for the specified range of this styled string. 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ciAfter a style is removed, the value set for the corresponding style attribute in the [Text](./ts-basic-components-text.md) component is used. If the value is not set, the default value is used. 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ciThis API equally works when the styled string contains an image. 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ciremoveStyle(start: number , length: number , styledKey: StyledStringKey): void 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci**Parameters** 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 317e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 318e41f4b71Sopenharmony_ci| start | number | Yes | Subscript that corresponds to the start position of the target range.| 319e41f4b71Sopenharmony_ci| length | number | Yes | Length of the target range.| 320e41f4b71Sopenharmony_ci| styledKey | [StyledStringKey](#styledstringkey12) | Yes | Styled key.| 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci**Error codes** 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci| ID| Error Message| 327e41f4b71Sopenharmony_ci| ------- | -------- | 328e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci### removeStyles 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ciRemoves all styles for the specified range of this styled string. 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ciAfter a style is removed, the value set for the corresponding style attribute in the [Text](./ts-basic-components-text.md) component is used. If the value is not set, the default value is used. 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ciThis API equally works when the styled string contains an image. 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ciremoveStyles(start: number , length: number): void 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**Parameters** 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 347e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 348e41f4b71Sopenharmony_ci| start | number | Yes | Subscript that corresponds to the start position of the target range.| 349e41f4b71Sopenharmony_ci| length | number | Yes | Length of the target range.| 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**Error codes** 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci| ID| Error Message| 356e41f4b71Sopenharmony_ci| ------- | -------- | 357e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci### clearStyles 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ciRemoves all styles of this styled string. 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ciAfter a style is removed, the value set for the corresponding style attribute in the [Text](./ts-basic-components-text.md) component is used. If the value is not set, the default value is used. 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ciclearStyles(): void 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci### replaceStyledString 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ciReplaces the styled string in the specified range. 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_cireplaceStyledString(start: number , length: number , other: StyledString): void 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**Parameters** 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 384e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 385e41f4b71Sopenharmony_ci| start | number | Yes | Subscript that corresponds to the start position of the target range.| 386e41f4b71Sopenharmony_ci| length | number | Yes | Length of the target range.| 387e41f4b71Sopenharmony_ci| other | [StyledString](#styledstring) | Yes | New styled string.| 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci**Error codes** 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci| ID| Error Message| 394e41f4b71Sopenharmony_ci| ------- | -------- | 395e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci### insertStyledString 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ciInserts a new styled string at the specified position. 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ciinsertStyledString(start: number , other: StyledString): void 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ci**Parameters** 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 410e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 411e41f4b71Sopenharmony_ci| start | number | Yes | Subscript of the position to insert the styled string.| 412e41f4b71Sopenharmony_ci| other | [StyledString](#styledstring) | Yes | New styled string.| 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci**Error codes** 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../../errorcode-universal.md). 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci| ID| Error Message| 419e41f4b71Sopenharmony_ci| ------- | -------- | 420e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci### appendStyledString 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ciAppends a styled string. 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ciappendStyledString(other: StyledString): void 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci**Parameters** 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 435e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 436e41f4b71Sopenharmony_ci| other | [StyledString](#styledstring) | Yes | New styled string.| 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci## StyleOptions 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 443e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 444e41f4b71Sopenharmony_ci| start | number | No | Start position of the styled string style.| 445e41f4b71Sopenharmony_ci| length | number | No | Length of the styled string style.| 446e41f4b71Sopenharmony_ci| styledKey | [StyledStringKey](#styledstringkey12) | Yes | Style key.| 447e41f4b71Sopenharmony_ci| styledValue | [StyledStringValue](ts-types.md#styledstringvalue12) | Yes | Style object.| 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci## SpanStyle 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 454e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 455e41f4b71Sopenharmony_ci| start | number | Yes | Start position of the styled string style.| 456e41f4b71Sopenharmony_ci| length | number | Yes | Length of the styled string style.| 457e41f4b71Sopenharmony_ci| styledKey | [StyledStringKey](#styledstringkey12) | Yes | Style key.| 458e41f4b71Sopenharmony_ci| styledValue | [StyledStringValue](ts-types.md#styledstringvalue12) | Yes | Style object.| 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci## TextStyle 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ciDescribes the text font style. 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ci### Attributes 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 473e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 474e41f4b71Sopenharmony_ci| fontColor | [ResourceColor](ts-types.md#resourcecolor) | Yes | No | Font color of the styled string.| 475e41f4b71Sopenharmony_ci| fontFamily | string | Yes | No | Font family of the styled string.| 476e41f4b71Sopenharmony_ci| fontSize | number | Yes | No | Font size of the styled string.<br>Unit: fp| 477e41f4b71Sopenharmony_ci| fontWeight | number | Yes | No | Font weight of the styled string.| 478e41f4b71Sopenharmony_ci| fontStyle | [FontStyle](ts-appendix-enums.md#fontstyle) | Yes | No | Font style of the styled string.| 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci### constructor 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ciconstructor(value?: TextStyleInterface) 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci**Parameters** 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 491e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 492e41f4b71Sopenharmony_ci| value | [TextStyleInterface](#textstyleinterface) | No | Font style options.| 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci## TextStyleInterface 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 499e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 500e41f4b71Sopenharmony_ci| fontColor | [ResourceColor](ts-types.md#resourcecolor) | No | Font color.| 501e41f4b71Sopenharmony_ci| fontFamily | [ResourceStr](ts-types.md#resourcestr) | No | Font family.| 502e41f4b71Sopenharmony_ci| fontSize | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No | Font size. If **unit** of **LengthMetrics** is percent, the setting does not take effect, and 16 fp is used.| 503e41f4b71Sopenharmony_ci| fontWeight | number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string | No | Font weight.| 504e41f4b71Sopenharmony_ci| fontStyle | [FontStyle](ts-appendix-enums.md#fontstyle) | No | Font style.| 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci## GestureStyle 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ciDescribes the event gesture style. 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci### constructor 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_ciconstructor(value?: GestureStyleInterface) 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci**Parameters** 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 523e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 524e41f4b71Sopenharmony_ci| value | [GestureStyleInterface](#gesturestyleinterface) | No | Event options.| 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci## GestureStyleInterface 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 531e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 532e41f4b71Sopenharmony_ci| onClick | CallBack\<[ClickEvent](ts-universal-events-click.md#clickevent)> | No | Callback for click events.| 533e41f4b71Sopenharmony_ci| onLongPress | CallBack\<[GestureEvent](./ts-gesture-settings.md#gestureevent)> | No | Callback for long press events.| 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci## DecorationStyle 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ciDescribes the text decorative line style. 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ci### Attributes 542e41f4b71Sopenharmony_ci 543e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 546e41f4b71Sopenharmony_ci 547e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 548e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 549e41f4b71Sopenharmony_ci| type | [TextDecorationType](ts-appendix-enums.md#textdecorationtype) | Yes | Yes | Type of the text decorative line.| 550e41f4b71Sopenharmony_ci| color | [ResourceColor](ts-types.md#resourcecolor) | Yes | No | Color of the text decorative line.| 551e41f4b71Sopenharmony_ci| style | [TextDecorationStyle](ts-appendix-enums.md#textdecorationstyle12) | Yes | No | Style of the text decorative line.| 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ci### constructor 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ciconstructor(value: DecorationStyleInterface) 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci**Parameters** 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 564e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 565e41f4b71Sopenharmony_ci| value | [DecorationStyleInterface](#decorationstyleinterface) | Yes | Text decorative line options.| 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci## DecorationStyleInterface 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 570e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 571e41f4b71Sopenharmony_ci| type | [TextDecorationType](ts-appendix-enums.md#textdecorationtype) | Yes | Type of the text decorative line.| 572e41f4b71Sopenharmony_ci| color | [ResourceColor](ts-types.md#resourcecolor) | No | Color of the text decorative line.| 573e41f4b71Sopenharmony_ci| style | [TextDecorationStyle](ts-appendix-enums.md#textdecorationstyle12) | No | Style of the text decorative line.| 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ci## DecorationStyleResult 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ciProvides the text decorative line information returned by the backend. 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci**Parameters** 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 584e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 585e41f4b71Sopenharmony_ci| type | [TextDecorationType](ts-appendix-enums.md#textdecorationtype) | Yes | Type of the text decorative line.| 586e41f4b71Sopenharmony_ci| color | [ResourceColor](ts-types.md#resourcecolor) | Yes | Color of the text decorative line.| 587e41f4b71Sopenharmony_ci| style | [TextDecorationStyle](ts-appendix-enums.md#textdecorationstyle12) | No | Style of the text decorative line.| 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci## BaselineOffsetStyle 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ciDescribes the text baseline offset style. 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci### Attributes 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 598e41f4b71Sopenharmony_ci 599e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 602e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 603e41f4b71Sopenharmony_ci| baselineOffset | number | Yes | Yes | Text baseline offset.<br>Unit: vp| 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ci### constructor 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ciconstructor(value: LengthMetrics) 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ci**Parameters** 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 616e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 617e41f4b71Sopenharmony_ci| value | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | Yes | Text baseline offset options. This API does not work if **unit** of **LengthMetrics** is percent.| 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci## LetterSpacingStyle 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ciDescribes the letter spacing style. 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci### Attributes 626e41f4b71Sopenharmony_ci 627e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 632e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 633e41f4b71Sopenharmony_ci| letterSpacing | number | Yes | Yes | Letter spacing.<br>Unit: vp| 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci### constructor 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ciconstructor(value: LengthMetrics) 638e41f4b71Sopenharmony_ci 639e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 640e41f4b71Sopenharmony_ci 641e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 642e41f4b71Sopenharmony_ci 643e41f4b71Sopenharmony_ci**Parameters** 644e41f4b71Sopenharmony_ci 645e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 646e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 647e41f4b71Sopenharmony_ci| value | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | Yes | Letter spacing options. This API does not work if **unit** of **LengthMetrics** is percent.| 648e41f4b71Sopenharmony_ci 649e41f4b71Sopenharmony_ci## LineHeightStyle 650e41f4b71Sopenharmony_ci 651e41f4b71Sopenharmony_ciDescribes the text line height style. 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_ci### Attributes 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 662e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 663e41f4b71Sopenharmony_ci| lineHeight | number | Yes | Yes | Text line height of the styled string.<br>Unit: vp| 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci### constructor 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ciconstructor(lineHeight: LengthMetrics) 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ci**Parameters** 674e41f4b71Sopenharmony_ci 675e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 676e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 677e41f4b71Sopenharmony_ci| lineHeight | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | Yes | Text line height options. If **value** of **LengthMetrics** is less than or equal to 0, the text line height is unlimited and automatically adapts to the font size.| 678e41f4b71Sopenharmony_ci 679e41f4b71Sopenharmony_ci## TextShadowStyle 680e41f4b71Sopenharmony_ci 681e41f4b71Sopenharmony_ciDescribes the text shadow style. 682e41f4b71Sopenharmony_ci 683e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci### Attributes 686e41f4b71Sopenharmony_ci 687e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 688e41f4b71Sopenharmony_ci 689e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 690e41f4b71Sopenharmony_ci 691e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 692e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 693e41f4b71Sopenharmony_ci| textShadow | Array\<[ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions)> | Yes | Yes | Text shadow of the styled string.| 694e41f4b71Sopenharmony_ci 695e41f4b71Sopenharmony_ci### constructor 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ciconstructor(value: ShadowOptions | Array\<ShadowOptions>) 698e41f4b71Sopenharmony_ci 699e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 700e41f4b71Sopenharmony_ci 701e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 702e41f4b71Sopenharmony_ci 703e41f4b71Sopenharmony_ci**Parameters** 704e41f4b71Sopenharmony_ci 705e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 706e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 707e41f4b71Sopenharmony_ci| value | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) \| Array\<[ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions)> | Yes | Text shadow options.| 708e41f4b71Sopenharmony_ci 709e41f4b71Sopenharmony_ci## ImageAttachment 710e41f4b71Sopenharmony_ci 711e41f4b71Sopenharmony_ciDescribes the image attachment. 712e41f4b71Sopenharmony_ci 713e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 714e41f4b71Sopenharmony_ci 715e41f4b71Sopenharmony_ci### Attributes 716e41f4b71Sopenharmony_ci 717e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 722e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 723e41f4b71Sopenharmony_ci| value | [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | Yes | Image data source of the styled string.| 724e41f4b71Sopenharmony_ci| size | [SizeOptions](ts-types.md#sizeoptions) | Yes | No | Image size of the styled string.| 725e41f4b71Sopenharmony_ci| verticalAlign | [ImageSpanAlignment](ts-basic-components-imagespan.md#imagespanalignment) | Yes | No | Image alignment mode of the styled string.| 726e41f4b71Sopenharmony_ci| objectFit | [ImageFit](ts-appendix-enums.md#imagefit) | Yes | No | Image scale type of the styled string.| 727e41f4b71Sopenharmony_ci| layoutStyle | [ImageAttachmentLayoutStyle](#imageattachmentlayoutstyle) | Yes | No | Image layout of the styled string.| 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci### constructor 730e41f4b71Sopenharmony_ci 731e41f4b71Sopenharmony_ciconstructor(value: ImageAttachmentInterface) 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 734e41f4b71Sopenharmony_ci 735e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_ci**Parameters** 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 740e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 741e41f4b71Sopenharmony_ci| value | [ImageAttachmentInterface](#imageattachmentinterface) | Yes | Image attachment options.| 742e41f4b71Sopenharmony_ci 743e41f4b71Sopenharmony_ci## ImageAttachmentInterface 744e41f4b71Sopenharmony_ci 745e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 746e41f4b71Sopenharmony_ci 747e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 748e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 749e41f4b71Sopenharmony_ci| value | [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | Image data source.| 750e41f4b71Sopenharmony_ci| size | [SizeOptions](ts-types.md#sizeoptions) | No | Image size.| 751e41f4b71Sopenharmony_ci| verticalAlign | [ImageSpanAlignment](ts-basic-components-imagespan.md#imagespanalignment) | No | Alignment mode of the image with the text.| 752e41f4b71Sopenharmony_ci| objectFit | [ImageFit](ts-appendix-enums.md#imagefit) | No | Image scale type.| 753e41f4b71Sopenharmony_ci| layoutStyle | [ImageAttachmentLayoutStyle](#imageattachmentlayoutstyle) | No | Image layout.| 754e41f4b71Sopenharmony_ci 755e41f4b71Sopenharmony_ci## ImageAttachmentLayoutStyle 756e41f4b71Sopenharmony_ci 757e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 760e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 761e41f4b71Sopenharmony_ci| margin | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) \| [Margin](ts-types.md#margin) | No | Image margin.| 762e41f4b71Sopenharmony_ci| padding | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) \| [Padding](ts-types.md#padding) | No | Image padding.| 763e41f4b71Sopenharmony_ci| borderRadius | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) \| [BorderRadiuses](ts-types.md#borderradiuses9) | No | Radius of the image border corners.| 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ci## CustomSpan 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_ciDescribes the custom span. Only the base class is provided. You need to define the specific implementation. 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ciThe drag preview of a custom span is blank. 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ci### onMeasure 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_ciCalled to obtain the size of a custom span. 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ciabstract onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics 778e41f4b71Sopenharmony_ci 779e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ci**Parameters** 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 786e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 787e41f4b71Sopenharmony_ci| measureInfo | [CustomSpanMeasureInfo](#customspanmeasureinfo) | Yes | Font size of the text.| 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ci**Return value** 790e41f4b71Sopenharmony_ci 791e41f4b71Sopenharmony_ci| Type | Description | 792e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 793e41f4b71Sopenharmony_ci| [CustomSpanMetrics](#customspanmetrics) | Size of the custom span.<br>**NOTE**<br>The final height of the custom span is subject to the line height of the **Text** component. If no value is specified for **height**, the custom span takes the **fontSize** value of the **Text** component as its height. If the value specified is greater than the height of other child components on the same line, the custom span takes the line height of the **Text** component as its height.| 794e41f4b71Sopenharmony_ci 795e41f4b71Sopenharmony_ci### onDraw 796e41f4b71Sopenharmony_ci 797e41f4b71Sopenharmony_ciCalled to draw a custom span. 798e41f4b71Sopenharmony_ci 799e41f4b71Sopenharmony_ciabstract onDraw(context: DrawContext, drawInfo: CustomSpanDrawInfo): void 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 802e41f4b71Sopenharmony_ci 803e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 804e41f4b71Sopenharmony_ci 805e41f4b71Sopenharmony_ci**Parameters** 806e41f4b71Sopenharmony_ci 807e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 808e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ | 809e41f4b71Sopenharmony_ci| context | [DrawContext](../js-apis-arkui-graphics.md#drawcontext) | Yes | Drawing context.<br>**NOTE**<br>The **canvas** method of **DrawContext** obtains the canvas of the **Text** component. As such, the custom span does not extend beyond the area of the **Text** component.| 810e41f4b71Sopenharmony_ci| drawInfo | [CustomSpanDrawInfo](#customspandrawinfo) | Yes | Drawing information of the custom span.| 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_ci## CustomSpanMeasureInfo 813e41f4b71Sopenharmony_ci 814e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 815e41f4b71Sopenharmony_ci 816e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 817e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 818e41f4b71Sopenharmony_ci| fontSize | number | Yes | Text font size.<br>Unit: fp| 819e41f4b71Sopenharmony_ci 820e41f4b71Sopenharmony_ci## CustomSpanMetrics 821e41f4b71Sopenharmony_ci 822e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 823e41f4b71Sopenharmony_ci 824e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 825e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 826e41f4b71Sopenharmony_ci| width | number | Yes | Width of the custom span.<br>Unit: vp| 827e41f4b71Sopenharmony_ci| height | number | No | Height of the custom span.<br>Unit: vp| 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci## CustomSpanDrawInfo 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 832e41f4b71Sopenharmony_ci 833e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 834e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 835e41f4b71Sopenharmony_ci| x | number | Yes | Offset of the custom span relative to the mounted component.<br>Unit: px| 836e41f4b71Sopenharmony_ci| lineTop | number | Yes | Top margin of the custom span relative to the **Text** component.<br>Unit: px| 837e41f4b71Sopenharmony_ci| lineBottom | number | Yes | Bottom margin of the custom span relative to the **Text** component.<br>Unit: px| 838e41f4b71Sopenharmony_ci| baseline | number | Yes | Baseline offset of the line where the custom span is located.<br>Unit: px| 839e41f4b71Sopenharmony_ci 840e41f4b71Sopenharmony_ci## ParagraphStyle 841e41f4b71Sopenharmony_ci 842e41f4b71Sopenharmony_ciDescribes the text paragraph style. 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ciExcept the first paragraph, all paragraphs are formed using the character escape '\n'. 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ciThe style of a paragraph is the one (if any) set for the first element or the paragraph style of the bound component. 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci### Attributes 851e41f4b71Sopenharmony_ci 852e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 853e41f4b71Sopenharmony_ci 854e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ci| Name | Type | Read Only | Mandatory | Description | 857e41f4b71Sopenharmony_ci| ------------ |---------------------| ---- | ---- | ------ | 858e41f4b71Sopenharmony_ci| textAlign | [TextAlign](ts-appendix-enums.md#textalign) | Yes | No | Horizontal alignment of the text paragraph.| 859e41f4b71Sopenharmony_ci| textIndent | number | Yes | No | First line indent of the text paragraph.| 860e41f4b71Sopenharmony_ci| maxLines | number | Yes | No | Maximum number of lines in the text paragraph.| 861e41f4b71Sopenharmony_ci| overflow | [TextOverflow](ts-appendix-enums.md#textoverflow) | Yes | No | Display mode when the text is too long in the text paragraph.| 862e41f4b71Sopenharmony_ci| wordBreak | [WordBreak](ts-appendix-enums.md#wordbreak11) | Yes | No | Word break rule of the text paragraph.| 863e41f4b71Sopenharmony_ci| leadingMargin | number \| [LeadingMarginPlaceholder](ts-basic-components-richeditor.md#leadingmarginplaceholder11) | Yes | No | Indent of the text paragraph.| 864e41f4b71Sopenharmony_ci 865e41f4b71Sopenharmony_ci> **NOTE** 866e41f4b71Sopenharmony_ci> 867e41f4b71Sopenharmony_ci> As the **maxLines** and **overflow** attributes of the styled string take effect only in the **Text** component, you are advised to set them in the component. 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci### constructor 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ciconstructor(value?: ParagraphStyleInterface) 872e41f4b71Sopenharmony_ci 873e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 874e41f4b71Sopenharmony_ci 875e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 876e41f4b71Sopenharmony_ci 877e41f4b71Sopenharmony_ci**Parameters** 878e41f4b71Sopenharmony_ci 879e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 880e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 881e41f4b71Sopenharmony_ci| value | [ParagraphStyleInterface](#paragraphstyleinterface) | No | Paragraph style options.| 882e41f4b71Sopenharmony_ci 883e41f4b71Sopenharmony_ci## ParagraphStyleInterface 884e41f4b71Sopenharmony_ci 885e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 886e41f4b71Sopenharmony_ci 887e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 888e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | --------------------------------- | 889e41f4b71Sopenharmony_ci| textAlign | [TextAlign](ts-appendix-enums.md#textalign) | No | Horizontal alignment of the text paragraph.| 890e41f4b71Sopenharmony_ci| textIndent | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No | First line indent of the text paragraph.| 891e41f4b71Sopenharmony_ci| maxLines | number | No | Maximum number of lines in the text paragraph.| 892e41f4b71Sopenharmony_ci| overflow | [TextOverflow](ts-appendix-enums.md#textoverflow) | No | Display mode when the text is too long in the text paragraph.<br>This parameter must be used with **maxLines** for the settings to take effect. **TextOverflow.MARQUEE** is not supported.| 893e41f4b71Sopenharmony_ci| wordBreak | [WordBreak](ts-appendix-enums.md#wordbreak11) | No | Word break rule of the text paragraph.| 894e41f4b71Sopenharmony_ci| leadingMargin | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) \| [LeadingMarginPlaceholder](ts-basic-components-richeditor.md#leadingmarginplaceholder11) | No | Indent of the text paragraph.| 895e41f4b71Sopenharmony_ci 896e41f4b71Sopenharmony_ci## UserDataSpan 897e41f4b71Sopenharmony_ci 898e41f4b71Sopenharmony_ciImplements a **UserDataSpan** object for storing and obtaining user data. Only the base class is provided. You need to define the specific implementation. 899e41f4b71Sopenharmony_ci 900e41f4b71Sopenharmony_ciThe extended user data does not affect the display effect. 901e41f4b71Sopenharmony_ci 902e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 903e41f4b71Sopenharmony_ci 904e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 905e41f4b71Sopenharmony_ci 906e41f4b71Sopenharmony_ci## StyledStringKey<sup>12+</sup> 907e41f4b71Sopenharmony_ci 908e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 909e41f4b71Sopenharmony_ci 910e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 911e41f4b71Sopenharmony_ci 912e41f4b71Sopenharmony_ci| Name | Description | 913e41f4b71Sopenharmony_ci| ------ | ----------------------------- | 914e41f4b71Sopenharmony_ci| FONT | Font style key, applicable to [TextStyle](./ts-universal-styled-string.md#textstyle).| 915e41f4b71Sopenharmony_ci| DECORATION | Text decorative line style key, applicable to [DecorationStyle](./ts-universal-styled-string.md#decorationstyle).| 916e41f4b71Sopenharmony_ci| BASELINE_OFFSET | Text baseline offset style key, applicable to [BaselineOffsetStyle](./ts-universal-styled-string.md#baselineoffsetstyle).| 917e41f4b71Sopenharmony_ci| LETTER_SPACING | Text letter spacing style key, applicable to [LetterSpacingStyle](./ts-universal-styled-string.md#letterspacingstyle).| 918e41f4b71Sopenharmony_ci| LINE_HEIGHT | Text line height style key, applicable to [LineHeightStyle](./ts-universal-styled-string.md#lineheightstyle).| 919e41f4b71Sopenharmony_ci| TEXT_SHADOW | Text shadow style key, applicable to [TextShadowStyle](./ts-universal-styled-string.md#textshadowstyle).| 920e41f4b71Sopenharmony_ci| GESTURE | Gesture key, applicable to [GestureStyle](./ts-universal-styled-string.md#gesturestyle).| 921e41f4b71Sopenharmony_ci| PARAGRAPH_STYLE | Paragraph style key, applicable to [ParagraphStyle](./ts-universal-styled-string.md#paragraphstyle).| 922e41f4b71Sopenharmony_ci| IMAGE | Image key, applicable to [ImageAttachment](./ts-universal-styled-string.md#imageattachment).| 923e41f4b71Sopenharmony_ci| CUSTOM_SPAN | Custom span key, applicable to [CustomSpan](./ts-universal-styled-string.md#customspan).| 924e41f4b71Sopenharmony_ci| USER_DATA | User data span key, applicable to [UserDataSpan](./ts-universal-styled-string.md#userdataspan).| 925e41f4b71Sopenharmony_ci 926e41f4b71Sopenharmony_ci## Example 927e41f4b71Sopenharmony_ci 928e41f4b71Sopenharmony_ci### Example 1 929e41f4b71Sopenharmony_ci 930e41f4b71Sopenharmony_ciThis example shows the basic usage of a styled string. 931e41f4b71Sopenharmony_ci 932e41f4b71Sopenharmony_ci```ts 933e41f4b71Sopenharmony_ci// xxx.ets 934e41f4b71Sopenharmony_ci@Entry 935e41f4b71Sopenharmony_ci@Component 936e41f4b71Sopenharmony_cistruct styled_string_demo1 { 937e41f4b71Sopenharmony_ci @State height1: number = 450; 938e41f4b71Sopenharmony_ci @State fontSize1: number = 16; 939e41f4b71Sopenharmony_ci @State fontWeight1: number = 400; 940e41f4b71Sopenharmony_ci @State color1: Color = Color.Blue; 941e41f4b71Sopenharmony_ci scroll: Scroller = new Scroller(); 942e41f4b71Sopenharmony_ci fontStyleAttr1: TextStyle = new TextStyle({ fontColor: Color.Blue }); 943e41f4b71Sopenharmony_ci fontStyleAttr2: StyledStringValue = new TextStyle({ fontColor: Color.Orange }); 944e41f4b71Sopenharmony_ci // Create a readable and writable styled string object: mutableStyledString1. 945e41f4b71Sopenharmony_ci mutableStyledString1: MutableStyledString = new MutableStyledString ("45-minute workout"); 946e41f4b71Sopenharmony_ci // Create the mutableStyledString2 object whose input parameters contain strings and styles. 947e41f4b71Sopenharmony_ci mutableStyledString2: MutableStyledString = new MutableStyledString("test hello world", [{ 948e41f4b71Sopenharmony_ci start: 0, 949e41f4b71Sopenharmony_ci length: 5, 950e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 951e41f4b71Sopenharmony_ci styledValue: this.fontStyleAttr1 952e41f4b71Sopenharmony_ci }]); 953e41f4b71Sopenharmony_ci // Create a read-only styled string object: styledString2. 954e41f4b71Sopenharmony_ci styledString2: StyledString = new StyledString("45-minute workout"); 955e41f4b71Sopenharmony_ci spanStyle1: SpanStyle = { 956e41f4b71Sopenharmony_ci start: 0, 957e41f4b71Sopenharmony_ci length: 5, 958e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 959e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Pink }) 960e41f4b71Sopenharmony_ci }; 961e41f4b71Sopenharmony_ci spanStyle2: SpanStyle = { 962e41f4b71Sopenharmony_ci start: 0, 963e41f4b71Sopenharmony_ci length: 2, 964e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 965e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Red }) 966e41f4b71Sopenharmony_ci }; 967e41f4b71Sopenharmony_ci @State string1: string = ''; 968e41f4b71Sopenharmony_ci @State fontColor1: ResourceColor = Color.Red; 969e41f4b71Sopenharmony_ci controller1: TextController = new TextController(); 970e41f4b71Sopenharmony_ci controller2: TextController = new TextController(); 971e41f4b71Sopenharmony_ci controller3: TextController = new TextController(); 972e41f4b71Sopenharmony_ci 973e41f4b71Sopenharmony_ci async onPageShow() { 974e41f4b71Sopenharmony_ci this.controller1.setStyledString(this.styledString2) 975e41f4b71Sopenharmony_ci this.controller2.setStyledString(this.mutableStyledString1) 976e41f4b71Sopenharmony_ci this.controller3.setStyledString(this.mutableStyledString2) 977e41f4b71Sopenharmony_ci } 978e41f4b71Sopenharmony_ci 979e41f4b71Sopenharmony_ci build() { 980e41f4b71Sopenharmony_ci Column() { 981e41f4b71Sopenharmony_ci Scroll(this.scroll) { 982e41f4b71Sopenharmony_ci Column() { 983e41f4b71Sopenharmony_ci // Display the styled string. 984e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller1 }) 985e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller2 }).key('mutableStyledString1') 986e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller3 }).key('mutableStyledString2') 987e41f4b71Sopenharmony_ci Button('Change string1 Value') 988e41f4b71Sopenharmony_ci .onClick(() => { 989e41f4b71Sopenharmony_ci let result = this.mutableStyledString1.equals(this.styledString2); 990e41f4b71Sopenharmony_ci if (result) { 991e41f4b71Sopenharmony_ci this.string1 = this.mutableStyledString1.getString(); 992e41f4b71Sopenharmony_ci console.info("mutableStyledString1 content:", this.mutableStyledString1.getString()); 993e41f4b71Sopenharmony_ci console.info("mutableStyledString1 length:", this.mutableStyledString1.length); 994e41f4b71Sopenharmony_ci } 995e41f4b71Sopenharmony_ci }) 996e41f4b71Sopenharmony_ci 997e41f4b71Sopenharmony_ci // If the styled string conflicts with the span, the span is ignored. The attributes of the Text component take effect if they do not conflict with the styled string. 998e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller2 }) { 999e41f4b71Sopenharmony_ci Span("span and styledString test") 1000e41f4b71Sopenharmony_ci .fontColor(Color.Yellow) 1001e41f4b71Sopenharmony_ci .decoration({ type: TextDecorationType.LineThrough }) 1002e41f4b71Sopenharmony_ci ImageSpan($r('app.media.icon')) 1003e41f4b71Sopenharmony_ci } 1004e41f4b71Sopenharmony_ci .key('styledString2') 1005e41f4b71Sopenharmony_ci .fontColor(this.fontColor1) 1006e41f4b71Sopenharmony_ci .letterSpacing(10) 1007e41f4b71Sopenharmony_ci .fontSize(32) 1008e41f4b71Sopenharmony_ci .fontWeight(600) 1009e41f4b71Sopenharmony_ci .fontStyle(FontStyle.Italic) 1010e41f4b71Sopenharmony_ci .lineHeight(30) 1011e41f4b71Sopenharmony_ci .textShadow({ radius: 5, color: Color.Blue, offsetX: 5, offsetY: 5 }) 1012e41f4b71Sopenharmony_ci .textCase(TextCase.UpperCase) 1013e41f4b71Sopenharmony_ci .decoration({ type: TextDecorationType.LineThrough, color: Color.Yellow }) 1014e41f4b71Sopenharmony_ci .baselineOffset(2) 1015e41f4b71Sopenharmony_ci .copyOption(CopyOptions.InApp) 1016e41f4b71Sopenharmony_ci .margin({ top: 10 }) 1017e41f4b71Sopenharmony_ci .draggable(true) 1018e41f4b71Sopenharmony_ci 1019e41f4b71Sopenharmony_ci // The following is for comparison with the preceding. 1020e41f4b71Sopenharmony_ci Text() { 1021e41f4b71Sopenharmony_ci Span(this.string1) 1022e41f4b71Sopenharmony_ci .fontColor(this.color1) 1023e41f4b71Sopenharmony_ci .decoration({ type: TextDecorationType.LineThrough }) 1024e41f4b71Sopenharmony_ci ImageSpan($r('app.media.icon')) 1025e41f4b71Sopenharmony_ci .width(50).height(50) 1026e41f4b71Sopenharmony_ci } 1027e41f4b71Sopenharmony_ci .letterSpacing(10) 1028e41f4b71Sopenharmony_ci .fontSize(32) 1029e41f4b71Sopenharmony_ci .fontWeight(600) 1030e41f4b71Sopenharmony_ci .fontStyle(FontStyle.Italic) 1031e41f4b71Sopenharmony_ci .lineHeight(30) 1032e41f4b71Sopenharmony_ci .textShadow({ radius: 5, color: Color.Blue, offsetX: 5, offsetY: 5 }) 1033e41f4b71Sopenharmony_ci .textCase(TextCase.UpperCase) 1034e41f4b71Sopenharmony_ci .decoration({ type: TextDecorationType.LineThrough, color: Color.Yellow }) 1035e41f4b71Sopenharmony_ci .baselineOffset(2) 1036e41f4b71Sopenharmony_ci 1037e41f4b71Sopenharmony_ci Button('Set Style and Replace Text') 1038e41f4b71Sopenharmony_ci .onClick(() => { 1039e41f4b71Sopenharmony_ci this.mutableStyledString1.replaceStyle({ 1040e41f4b71Sopenharmony_ci start: 2, 1041e41f4b71Sopenharmony_ci length: 2, 1042e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1043e41f4b71Sopenharmony_ci styledValue: this.fontStyleAttr1 1044e41f4b71Sopenharmony_ci }) 1045e41f4b71Sopenharmony_ci this.mutableStyledString1.insertString(0, "Blood Pressure: 85 (High)") 1046e41f4b71Sopenharmony_ci this.mutableStyledString1.setStyle({ 1047e41f4b71Sopenharmony_ci start: 2, 1048e41f4b71Sopenharmony_ci length: 2, 1049e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1050e41f4b71Sopenharmony_ci styledValue: this.fontStyleAttr2 1051e41f4b71Sopenharmony_ci }) 1052e41f4b71Sopenharmony_ci this.controller2.setStyledString(this.mutableStyledString1) 1053e41f4b71Sopenharmony_ci }) 1054e41f4b71Sopenharmony_ci .margin({ top: 10 }) 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_ci Button('Query and Clear Style') 1057e41f4b71Sopenharmony_ci .onClick(() => { 1058e41f4b71Sopenharmony_ci let styles = this.mutableStyledString1.getStyles(0, this.mutableStyledString1.length) 1059e41f4b71Sopenharmony_ci if (styles.length == 2) { 1060e41f4b71Sopenharmony_ci for (let i = 0; i < styles.length; i++) { 1061e41f4b71Sopenharmony_ci console.info('StyledString style object start:' + styles[i].start) 1062e41f4b71Sopenharmony_ci console.info('StyledString style object length:' + styles[i].length) 1063e41f4b71Sopenharmony_ci console.info('StyledString style object key:' + styles[i].styledKey) 1064e41f4b71Sopenharmony_ci if (styles[i].styledKey === 0) { 1065e41f4b71Sopenharmony_ci let fontAttr = styles[i].styledValue as TextStyle; 1066e41f4b71Sopenharmony_ci console.info('StyledString fontColor:' + fontAttr.fontColor) 1067e41f4b71Sopenharmony_ci } 1068e41f4b71Sopenharmony_ci } 1069e41f4b71Sopenharmony_ci } 1070e41f4b71Sopenharmony_ci if (styles[0] !== undefined) { 1071e41f4b71Sopenharmony_ci this.mutableStyledString2.setStyle(styles[0]); 1072e41f4b71Sopenharmony_ci this.controller3.setStyledString(this.mutableStyledString2); 1073e41f4b71Sopenharmony_ci } 1074e41f4b71Sopenharmony_ci this.mutableStyledString1.removeStyles(2, 3); 1075e41f4b71Sopenharmony_ci this.controller2.setStyledString(this.mutableStyledString1); 1076e41f4b71Sopenharmony_ci }) 1077e41f4b71Sopenharmony_ci .margin({ top: 10 }) 1078e41f4b71Sopenharmony_ci }.width('100%') 1079e41f4b71Sopenharmony_ci 1080e41f4b71Sopenharmony_ci } 1081e41f4b71Sopenharmony_ci .expandSafeArea([SafeAreaType.KEYBOARD]) 1082e41f4b71Sopenharmony_ci .scrollable(ScrollDirection.Vertical) 1083e41f4b71Sopenharmony_ci .scrollBar(BarState.On) 1084e41f4b71Sopenharmony_ci .scrollBarColor(Color.Gray) 1085e41f4b71Sopenharmony_ci .scrollBarWidth(10) 1086e41f4b71Sopenharmony_ci .edgeEffect(EdgeEffect.None) 1087e41f4b71Sopenharmony_ci } 1088e41f4b71Sopenharmony_ci .width('100%') 1089e41f4b71Sopenharmony_ci } 1090e41f4b71Sopenharmony_ci} 1091e41f4b71Sopenharmony_ci``` 1092e41f4b71Sopenharmony_ci 1093e41f4b71Sopenharmony_ci 1094e41f4b71Sopenharmony_ci 1095e41f4b71Sopenharmony_ci### Example 2 1096e41f4b71Sopenharmony_ci 1097e41f4b71Sopenharmony_ciThis example shows a styled string that supports events. 1098e41f4b71Sopenharmony_ci 1099e41f4b71Sopenharmony_ci```ts 1100e41f4b71Sopenharmony_ci// xxx.ets 1101e41f4b71Sopenharmony_ciimport { promptAction } from '@kit.ArkUI'; 1102e41f4b71Sopenharmony_ci 1103e41f4b71Sopenharmony_ci@Entry 1104e41f4b71Sopenharmony_ci@Component 1105e41f4b71Sopenharmony_cistruct styled_string_demo2 { 1106e41f4b71Sopenharmony_ci scroll: Scroller = new Scroller(); 1107e41f4b71Sopenharmony_ci fontStyleAttr1: TextStyle = new TextStyle({ fontColor: Color.Blue }); 1108e41f4b71Sopenharmony_ci clickGestureAttr: GestureStyle = new GestureStyle({ 1109e41f4b71Sopenharmony_ci onClick: () => { 1110e41f4b71Sopenharmony_ci promptAction.showToast({ message: 'clickGestureAttr object trigger click event' }) 1111e41f4b71Sopenharmony_ci this.backgroundColor1 = Color.Yellow 1112e41f4b71Sopenharmony_ci } 1113e41f4b71Sopenharmony_ci }) 1114e41f4b71Sopenharmony_ci gestureStyleAttr: GestureStyle = new GestureStyle({ 1115e41f4b71Sopenharmony_ci onClick: () => { 1116e41f4b71Sopenharmony_ci promptAction.showToast({ message: 'gestureStyleAttr object trigger click event' }) 1117e41f4b71Sopenharmony_ci this.backgroundColor1 = Color.Green 1118e41f4b71Sopenharmony_ci }, 1119e41f4b71Sopenharmony_ci onLongPress: () => { 1120e41f4b71Sopenharmony_ci promptAction.showToast({ message: 'gestureStyleAttr object trigger long press event' }) 1121e41f4b71Sopenharmony_ci this.backgroundColor1 = Color.Orange 1122e41f4b71Sopenharmony_ci } 1123e41f4b71Sopenharmony_ci }); 1124e41f4b71Sopenharmony_ci // Create the event object mutableStyledString3. 1125e41f4b71Sopenharmony_ci mutableStyledString3: MutableStyledString = new MutableStyledString("hello world", [{ 1126e41f4b71Sopenharmony_ci start: 0, 1127e41f4b71Sopenharmony_ci length: 5, 1128e41f4b71Sopenharmony_ci styledKey: StyledStringKey.GESTURE, 1129e41f4b71Sopenharmony_ci styledValue: this.clickGestureAttr 1130e41f4b71Sopenharmony_ci }, 1131e41f4b71Sopenharmony_ci { 1132e41f4b71Sopenharmony_ci start: 0, 1133e41f4b71Sopenharmony_ci length: 5, 1134e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1135e41f4b71Sopenharmony_ci styledValue: this.fontStyleAttr1 1136e41f4b71Sopenharmony_ci }, 1137e41f4b71Sopenharmony_ci { 1138e41f4b71Sopenharmony_ci start: 6, 1139e41f4b71Sopenharmony_ci length: 5, 1140e41f4b71Sopenharmony_ci styledKey: StyledStringKey.GESTURE, 1141e41f4b71Sopenharmony_ci styledValue: this.gestureStyleAttr 1142e41f4b71Sopenharmony_ci }, 1143e41f4b71Sopenharmony_ci { 1144e41f4b71Sopenharmony_ci start: 6, 1145e41f4b71Sopenharmony_ci length: 5, 1146e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1147e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Pink }) 1148e41f4b71Sopenharmony_ci }]); 1149e41f4b71Sopenharmony_ci @State fontColor1: ResourceColor = Color.Red; 1150e41f4b71Sopenharmony_ci @State backgroundColor1: ResourceColor | undefined = undefined; 1151e41f4b71Sopenharmony_ci controller3: TextController = new TextController(); 1152e41f4b71Sopenharmony_ci 1153e41f4b71Sopenharmony_ci async onPageShow() { 1154e41f4b71Sopenharmony_ci this.controller3.setStyledString(this.mutableStyledString3) 1155e41f4b71Sopenharmony_ci } 1156e41f4b71Sopenharmony_ci 1157e41f4b71Sopenharmony_ci build() { 1158e41f4b71Sopenharmony_ci Column() { 1159e41f4b71Sopenharmony_ci Scroll(this.scroll) { 1160e41f4b71Sopenharmony_ci Column({ space: 30 }) { 1161e41f4b71Sopenharmony_ci Button("Change Background Color in Response to Event").backgroundColor(this.backgroundColor1).width('80%') 1162e41f4b71Sopenharmony_ci // Styled string that contains an event 1163e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller3 }).fontSize(30) 1164e41f4b71Sopenharmony_ci .copyOption(CopyOptions.InApp) 1165e41f4b71Sopenharmony_ci .draggable(true) 1166e41f4b71Sopenharmony_ci .clip(true) 1167e41f4b71Sopenharmony_ci }.width('100%') 1168e41f4b71Sopenharmony_ci } 1169e41f4b71Sopenharmony_ci .expandSafeArea([SafeAreaType.KEYBOARD]) 1170e41f4b71Sopenharmony_ci .scrollable(ScrollDirection.Vertical) 1171e41f4b71Sopenharmony_ci .scrollBar(BarState.On) 1172e41f4b71Sopenharmony_ci .scrollBarColor(Color.Gray) 1173e41f4b71Sopenharmony_ci .scrollBarWidth(10) 1174e41f4b71Sopenharmony_ci .edgeEffect(EdgeEffect.None) 1175e41f4b71Sopenharmony_ci } 1176e41f4b71Sopenharmony_ci .width('100%') 1177e41f4b71Sopenharmony_ci } 1178e41f4b71Sopenharmony_ci} 1179e41f4b71Sopenharmony_ci``` 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci 1182e41f4b71Sopenharmony_ci 1183e41f4b71Sopenharmony_ci### Example 3 1184e41f4b71Sopenharmony_ci 1185e41f4b71Sopenharmony_ciThis example shows a styled string that supports text styles. 1186e41f4b71Sopenharmony_ci 1187e41f4b71Sopenharmony_ci```ts 1188e41f4b71Sopenharmony_ci// xxx.ets 1189e41f4b71Sopenharmony_ciimport { LengthMetrics, LengthUnit } from '@kit.ArkUI' 1190e41f4b71Sopenharmony_ci 1191e41f4b71Sopenharmony_ci@Entry 1192e41f4b71Sopenharmony_ci@Component 1193e41f4b71Sopenharmony_cistruct styled_string_demo3 { 1194e41f4b71Sopenharmony_ci fontStyleAttr1: TextStyle = new TextStyle({ fontColor: Color.Blue }); 1195e41f4b71Sopenharmony_ci fontStyleAttr2: StyledStringValue = new TextStyle({ 1196e41f4b71Sopenharmony_ci fontColor: Color.Orange, 1197e41f4b71Sopenharmony_ci fontSize: LengthMetrics.vp(20), 1198e41f4b71Sopenharmony_ci fontWeight: FontWeight.Bolder, 1199e41f4b71Sopenharmony_ci fontStyle: FontStyle.Italic, 1200e41f4b71Sopenharmony_ci fontFamily: "Arial" 1201e41f4b71Sopenharmony_ci }); 1202e41f4b71Sopenharmony_ci fontStyleAttr3: StyledStringValue = new TextStyle({ 1203e41f4b71Sopenharmony_ci fontColor: Color.Orange, 1204e41f4b71Sopenharmony_ci fontSize: LengthMetrics.vp(20), 1205e41f4b71Sopenharmony_ci fontWeight: FontWeight.Lighter, 1206e41f4b71Sopenharmony_ci fontStyle: FontStyle.Italic, 1207e41f4b71Sopenharmony_ci fontFamily: "Arial" 1208e41f4b71Sopenharmony_ci }); 1209e41f4b71Sopenharmony_ci // Create a styled string object with multiple text styles: mutableStyledString1. 1210e41f4b71Sopenharmony_ci mutableStyledString1: MutableStyledString = new MutableStyledString("45-minute workout", [{ 1211e41f4b71Sopenharmony_ci start: 0, 1212e41f4b71Sopenharmony_ci length: 2, 1213e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1214e41f4b71Sopenharmony_ci styledValue: this.fontStyleAttr3 1215e41f4b71Sopenharmony_ci }, { 1216e41f4b71Sopenharmony_ci start: 2, 1217e41f4b71Sopenharmony_ci length: 2, 1218e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1219e41f4b71Sopenharmony_ci styledValue: this.fontStyleAttr2 1220e41f4b71Sopenharmony_ci } 1221e41f4b71Sopenharmony_ci ]); 1222e41f4b71Sopenharmony_ci // Create a styled string object with multiple styles: mutableStyledString2. 1223e41f4b71Sopenharmony_ci mutableStyledString2: MutableStyledString = new MutableStyledString("test hello world", [{ 1224e41f4b71Sopenharmony_ci start: 0, 1225e41f4b71Sopenharmony_ci length: 5, 1226e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1227e41f4b71Sopenharmony_ci styledValue: this.fontStyleAttr1 1228e41f4b71Sopenharmony_ci }, { 1229e41f4b71Sopenharmony_ci start: 0, 1230e41f4b71Sopenharmony_ci length: 5, 1231e41f4b71Sopenharmony_ci styledKey: StyledStringKey.DECORATION, 1232e41f4b71Sopenharmony_ci styledValue: new DecorationStyle({ type: TextDecorationType.LineThrough, color: Color.Blue }) 1233e41f4b71Sopenharmony_ci }, { 1234e41f4b71Sopenharmony_ci start: 0, 1235e41f4b71Sopenharmony_ci length: 5, 1236e41f4b71Sopenharmony_ci styledKey: StyledStringKey.TEXT_SHADOW, 1237e41f4b71Sopenharmony_ci styledValue: new TextShadowStyle({ 1238e41f4b71Sopenharmony_ci radius: 5, 1239e41f4b71Sopenharmony_ci type: ShadowType.COLOR, 1240e41f4b71Sopenharmony_ci color: Color.Yellow, 1241e41f4b71Sopenharmony_ci offsetX: 10, 1242e41f4b71Sopenharmony_ci offsetY: -10 1243e41f4b71Sopenharmony_ci }) 1244e41f4b71Sopenharmony_ci }, { 1245e41f4b71Sopenharmony_ci start: 0, 1246e41f4b71Sopenharmony_ci length: 5, 1247e41f4b71Sopenharmony_ci styledKey: StyledStringKey.BASELINE_OFFSET, 1248e41f4b71Sopenharmony_ci styledValue: new BaselineOffsetStyle(LengthMetrics.px(20)) 1249e41f4b71Sopenharmony_ci }, { 1250e41f4b71Sopenharmony_ci start: 0, 1251e41f4b71Sopenharmony_ci length: 5, 1252e41f4b71Sopenharmony_ci styledKey: StyledStringKey.LETTER_SPACING, 1253e41f4b71Sopenharmony_ci styledValue: new LetterSpacingStyle(new LengthMetrics(10, LengthUnit.VP)) 1254e41f4b71Sopenharmony_ci }, { 1255e41f4b71Sopenharmony_ci start: 6, 1256e41f4b71Sopenharmony_ci length: 5, 1257e41f4b71Sopenharmony_ci styledKey: StyledStringKey.BASELINE_OFFSET, 1258e41f4b71Sopenharmony_ci styledValue: new BaselineOffsetStyle(LengthMetrics.fp(10)) 1259e41f4b71Sopenharmony_ci } 1260e41f4b71Sopenharmony_ci ]); 1261e41f4b71Sopenharmony_ci @State fontColor1: ResourceColor = Color.Red; 1262e41f4b71Sopenharmony_ci controller: TextController = new TextController(); 1263e41f4b71Sopenharmony_ci options: TextOptions = { controller: this.controller }; 1264e41f4b71Sopenharmony_ci controller2: TextController = new TextController(); 1265e41f4b71Sopenharmony_ci spanStyle1: SpanStyle = { 1266e41f4b71Sopenharmony_ci start: 0, 1267e41f4b71Sopenharmony_ci length: 5, 1268e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1269e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Pink }) 1270e41f4b71Sopenharmony_ci }; 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci async onPageShow() { 1273e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStyledString1) 1274e41f4b71Sopenharmony_ci this.controller2.setStyledString(this.mutableStyledString2) 1275e41f4b71Sopenharmony_ci } 1276e41f4b71Sopenharmony_ci 1277e41f4b71Sopenharmony_ci build() { 1278e41f4b71Sopenharmony_ci Column() { 1279e41f4b71Sopenharmony_ci Column({ space: 10 }) { 1280e41f4b71Sopenharmony_ci // Display the styled string configured with various font styles. If the styled string conflicts with the style settings in the Text component, the style set in the styled string takes effect. 1281e41f4b71Sopenharmony_ci Text(undefined, this.options) 1282e41f4b71Sopenharmony_ci .fontColor(this.fontColor1) 1283e41f4b71Sopenharmony_ci .font({ size: 20, weight: 500, style: FontStyle.Normal }) 1284e41f4b71Sopenharmony_ci // Display the styled string for which the text shadow, text decorative line, letter spacing, and baseline offset are configured. If the styled string conflicts with the style settings in the Text component, the style set in the styled string takes effect. 1285e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller2 }) 1286e41f4b71Sopenharmony_ci .fontSize(30) 1287e41f4b71Sopenharmony_ci .copyOption(CopyOptions.InApp) 1288e41f4b71Sopenharmony_ci .draggable(true) 1289e41f4b71Sopenharmony_ci .decoration({ type: TextDecorationType.Overline, color: Color.Pink }) 1290e41f4b71Sopenharmony_ci .textShadow({ 1291e41f4b71Sopenharmony_ci radius: 10, 1292e41f4b71Sopenharmony_ci type: ShadowType.COLOR, 1293e41f4b71Sopenharmony_ci color: Color.Green, 1294e41f4b71Sopenharmony_ci offsetX: -10, 1295e41f4b71Sopenharmony_ci offsetY: 10 1296e41f4b71Sopenharmony_ci }) 1297e41f4b71Sopenharmony_ci Button('Query Font Style') 1298e41f4b71Sopenharmony_ci .onClick(() => { 1299e41f4b71Sopenharmony_ci let styles = this.mutableStyledString1.getStyles(0, this.mutableStyledString1.length) 1300e41f4b71Sopenharmony_ci if (styles.length !== 0) { 1301e41f4b71Sopenharmony_ci for (let i = 0; i < styles.length; i++) { 1302e41f4b71Sopenharmony_ci console.info('mutableStyledString1 style object start:' + styles[i].start) 1303e41f4b71Sopenharmony_ci console.info('mutableStyledString1 style object length:' + styles[i].length) 1304e41f4b71Sopenharmony_ci console.info('mutableStyledString1 style object key:' + styles[i].styledKey) 1305e41f4b71Sopenharmony_ci if (styles[i].styledKey === 0) { 1306e41f4b71Sopenharmony_ci let fontAttr = styles[i].styledValue as TextStyle; 1307e41f4b71Sopenharmony_ci console.info('mutableStyledString1 fontColor:' + fontAttr.fontColor) 1308e41f4b71Sopenharmony_ci console.info('mutableStyledString1 fontSize:' + fontAttr.fontSize) 1309e41f4b71Sopenharmony_ci console.info('mutableStyledString1 fontWeight:' + fontAttr.fontWeight) 1310e41f4b71Sopenharmony_ci console.info('mutableStyledString1 fontStyle:' + fontAttr.fontStyle) 1311e41f4b71Sopenharmony_ci console.info('mutableStyledString1 fontStyle:' + fontAttr.fontFamily) 1312e41f4b71Sopenharmony_ci } 1313e41f4b71Sopenharmony_ci } 1314e41f4b71Sopenharmony_ci } 1315e41f4b71Sopenharmony_ci }) 1316e41f4b71Sopenharmony_ci .margin({ top: 10 }) 1317e41f4b71Sopenharmony_ci Button('Query Other Styles') 1318e41f4b71Sopenharmony_ci .onClick(() => { 1319e41f4b71Sopenharmony_ci let styles = this.mutableStyledString2.getStyles(0, this.mutableStyledString2.length) 1320e41f4b71Sopenharmony_ci if (styles.length !== 0) { 1321e41f4b71Sopenharmony_ci for (let i = 0; i < styles.length; i++) { 1322e41f4b71Sopenharmony_ci console.info('mutableStyledString2 style object start:' + styles[i].start) 1323e41f4b71Sopenharmony_ci console.info('mutableStyledString2 style object length:' + styles[i].length) 1324e41f4b71Sopenharmony_ci console.info('mutableStyledString2 style object key:' + styles[i].styledKey) 1325e41f4b71Sopenharmony_ci if (styles[i].styledKey === 1) { 1326e41f4b71Sopenharmony_ci let decoAttr = styles[i].styledValue as DecorationStyle; 1327e41f4b71Sopenharmony_ci console.info('mutableStyledString2 decoration type:' + decoAttr.type) 1328e41f4b71Sopenharmony_ci console.info('mutableStyledString2 decoration color:' + decoAttr.color) 1329e41f4b71Sopenharmony_ci } 1330e41f4b71Sopenharmony_ci if (styles[i].styledKey === 2) { 1331e41f4b71Sopenharmony_ci let baselineAttr = styles[i].styledValue as BaselineOffsetStyle; 1332e41f4b71Sopenharmony_ci console.info('mutableStyledString2 baselineOffset:' + baselineAttr.baselineOffset) 1333e41f4b71Sopenharmony_ci } 1334e41f4b71Sopenharmony_ci if (styles[i].styledKey === 3) { 1335e41f4b71Sopenharmony_ci let letterAttr = styles[i].styledValue as LetterSpacingStyle; 1336e41f4b71Sopenharmony_ci console.info('mutableStyledString2 letterSpacing:' + letterAttr.letterSpacing) 1337e41f4b71Sopenharmony_ci } 1338e41f4b71Sopenharmony_ci if (styles[i].styledKey === 4) { 1339e41f4b71Sopenharmony_ci let textShadowAttr = styles[i].styledValue as TextShadowStyle; 1340e41f4b71Sopenharmony_ci let shadowValues = textShadowAttr.textShadow; 1341e41f4b71Sopenharmony_ci if (shadowValues.length > 0) { 1342e41f4b71Sopenharmony_ci for (let j = 0; j < shadowValues.length; j++) { 1343e41f4b71Sopenharmony_ci console.info('mutableStyledString2 textShadow type:' + shadowValues[j].type); 1344e41f4b71Sopenharmony_ci console.info('mutableStyledString2 textShadow radius:' + shadowValues[j].radius); 1345e41f4b71Sopenharmony_ci console.info('mutableStyledString2 textShadow color:' + shadowValues[j].color); 1346e41f4b71Sopenharmony_ci console.info('mutableStyledString2 textShadow offsetX:' + shadowValues[j].offsetX); 1347e41f4b71Sopenharmony_ci console.info('mutableStyledString2 textShadow offsetY:' + shadowValues[j].offsetY); 1348e41f4b71Sopenharmony_ci } 1349e41f4b71Sopenharmony_ci } 1350e41f4b71Sopenharmony_ci } 1351e41f4b71Sopenharmony_ci } 1352e41f4b71Sopenharmony_ci } 1353e41f4b71Sopenharmony_ci }) 1354e41f4b71Sopenharmony_ci .margin({ top: 10 }) 1355e41f4b71Sopenharmony_ci Button('Update mutableStyledString1 Style') 1356e41f4b71Sopenharmony_ci .onClick(() => { 1357e41f4b71Sopenharmony_ci this.mutableStyledString1.setStyle(this.spanStyle1) 1358e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStyledString1) 1359e41f4b71Sopenharmony_ci }) 1360e41f4b71Sopenharmony_ci .margin({ top: 10 }) 1361e41f4b71Sopenharmony_ci }.width('100%') 1362e41f4b71Sopenharmony_ci } 1363e41f4b71Sopenharmony_ci .width('100%') 1364e41f4b71Sopenharmony_ci } 1365e41f4b71Sopenharmony_ci} 1366e41f4b71Sopenharmony_ci``` 1367e41f4b71Sopenharmony_ci 1368e41f4b71Sopenharmony_ci 1369e41f4b71Sopenharmony_ci### Example 4 1370e41f4b71Sopenharmony_ci 1371e41f4b71Sopenharmony_ciThis example shows a styled string that contains an image. 1372e41f4b71Sopenharmony_ci 1373e41f4b71Sopenharmony_ci```ts 1374e41f4b71Sopenharmony_ci// xxx.ets 1375e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit' 1376e41f4b71Sopenharmony_ciimport { LengthMetrics } from '@kit.ArkUI' 1377e41f4b71Sopenharmony_ci 1378e41f4b71Sopenharmony_ci@Entry 1379e41f4b71Sopenharmony_ci@Component 1380e41f4b71Sopenharmony_cistruct styled_string_demo4 { 1381e41f4b71Sopenharmony_ci @State message: string = 'Hello World' 1382e41f4b71Sopenharmony_ci imagePixelMap: image.PixelMap | undefined = undefined 1383e41f4b71Sopenharmony_ci @State imagePixelMap3: image.PixelMap | undefined = undefined 1384e41f4b71Sopenharmony_ci mutableStr: MutableStyledString = new MutableStyledString('123'); 1385e41f4b71Sopenharmony_ci controller: TextController = new TextController(); 1386e41f4b71Sopenharmony_ci mutableStr2: MutableStyledString = new MutableStyledString('This is set decoration line style to the mutableStr2', [{ 1387e41f4b71Sopenharmony_ci start: 0, 1388e41f4b71Sopenharmony_ci length: 15, 1389e41f4b71Sopenharmony_ci styledKey: StyledStringKey.DECORATION, 1390e41f4b71Sopenharmony_ci styledValue: new DecorationStyle({ 1391e41f4b71Sopenharmony_ci type: TextDecorationType.Overline, 1392e41f4b71Sopenharmony_ci color: Color.Orange, 1393e41f4b71Sopenharmony_ci style: TextDecorationStyle.DOUBLE 1394e41f4b71Sopenharmony_ci }) 1395e41f4b71Sopenharmony_ci }]) 1396e41f4b71Sopenharmony_ci 1397e41f4b71Sopenharmony_ci async aboutToAppear() { 1398e41f4b71Sopenharmony_ci console.info("aboutToAppear initial imagePixelMap") 1399e41f4b71Sopenharmony_ci this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.icon')) 1400e41f4b71Sopenharmony_ci } 1401e41f4b71Sopenharmony_ci 1402e41f4b71Sopenharmony_ci private async getPixmapFromMedia(resource: Resource) { 1403e41f4b71Sopenharmony_ci let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({ 1404e41f4b71Sopenharmony_ci bundleName: resource.bundleName, 1405e41f4b71Sopenharmony_ci moduleName: resource.moduleName, 1406e41f4b71Sopenharmony_ci id: resource.id 1407e41f4b71Sopenharmony_ci }) 1408e41f4b71Sopenharmony_ci let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength)) 1409e41f4b71Sopenharmony_ci let createPixelMap: image.PixelMap = await imageSource.createPixelMap({ 1410e41f4b71Sopenharmony_ci desiredPixelFormat: image.PixelMapFormat.RGBA_8888 1411e41f4b71Sopenharmony_ci }) 1412e41f4b71Sopenharmony_ci await imageSource.release() 1413e41f4b71Sopenharmony_ci return createPixelMap 1414e41f4b71Sopenharmony_ci } 1415e41f4b71Sopenharmony_ci 1416e41f4b71Sopenharmony_ci build() { 1417e41f4b71Sopenharmony_ci Row() { 1418e41f4b71Sopenharmony_ci Column({ space: 5 }) { 1419e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller }) 1420e41f4b71Sopenharmony_ci .copyOption(CopyOptions.InApp) 1421e41f4b71Sopenharmony_ci .draggable(true) 1422e41f4b71Sopenharmony_ci .fontSize(30) 1423e41f4b71Sopenharmony_ci 1424e41f4b71Sopenharmony_ci Button('Set Image') 1425e41f4b71Sopenharmony_ci .onClick(() => { 1426e41f4b71Sopenharmony_ci if (this.imagePixelMap !== undefined) { 1427e41f4b71Sopenharmony_ci this.mutableStr = new MutableStyledString(new ImageAttachment({ 1428e41f4b71Sopenharmony_ci value: this.imagePixelMap, 1429e41f4b71Sopenharmony_ci size: { width: 50, height: 50 }, 1430e41f4b71Sopenharmony_ci layoutStyle: { borderRadius: LengthMetrics.vp(10) }, 1431e41f4b71Sopenharmony_ci verticalAlign: ImageSpanAlignment.BASELINE, 1432e41f4b71Sopenharmony_ci objectFit: ImageFit.Contain 1433e41f4b71Sopenharmony_ci })) 1434e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStr) 1435e41f4b71Sopenharmony_ci } 1436e41f4b71Sopenharmony_ci }) 1437e41f4b71Sopenharmony_ci 1438e41f4b71Sopenharmony_ci Button('Image: Append') 1439e41f4b71Sopenharmony_ci .onClick(() => { 1440e41f4b71Sopenharmony_ci let str = new StyledString('123') 1441e41f4b71Sopenharmony_ci this.mutableStr.appendStyledString(str) 1442e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStr) 1443e41f4b71Sopenharmony_ci }) 1444e41f4b71Sopenharmony_ci 1445e41f4b71Sopenharmony_ci Button('Image: Before Insert') 1446e41f4b71Sopenharmony_ci .onClick(() => { 1447e41f4b71Sopenharmony_ci this.mutableStr.insertString(0, '123') 1448e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStr) 1449e41f4b71Sopenharmony_ci }) 1450e41f4b71Sopenharmony_ci 1451e41f4b71Sopenharmony_ci Button('Image: After Insert') 1452e41f4b71Sopenharmony_ci .onClick(() => { 1453e41f4b71Sopenharmony_ci this.mutableStr.insertString(1, '123') 1454e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStr) 1455e41f4b71Sopenharmony_ci }) 1456e41f4b71Sopenharmony_ci 1457e41f4b71Sopenharmony_ci Button('Image: Replace') 1458e41f4b71Sopenharmony_ci .onClick(() => { 1459e41f4b71Sopenharmony_ci this.mutableStr.replaceString(2, 5, "789") 1460e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStr) 1461e41f4b71Sopenharmony_ci }) 1462e41f4b71Sopenharmony_ci 1463e41f4b71Sopenharmony_ci Button('Image: Get') 1464e41f4b71Sopenharmony_ci .onClick(() => { 1465e41f4b71Sopenharmony_ci let imageArray = this.mutableStr.getStyles(0, 1, StyledStringKey.IMAGE) 1466e41f4b71Sopenharmony_ci for (let i = 0; i < imageArray.length; ++i) { 1467e41f4b71Sopenharmony_ci console.info('mutableStr start ' + imageArray[i].start + ' length ' + imageArray[i].length + ' type ' + imageArray[i].styledKey) 1468e41f4b71Sopenharmony_ci if (imageArray[i].styledKey === 300) { 1469e41f4b71Sopenharmony_ci let attachment = imageArray[i].styledValue as ImageAttachment 1470e41f4b71Sopenharmony_ci this.imagePixelMap3 = attachment.value 1471e41f4b71Sopenharmony_ci console.info('mutableStr value ' + JSON.stringify(attachment.value)) 1472e41f4b71Sopenharmony_ci if (attachment.size !== undefined) { 1473e41f4b71Sopenharmony_ci console.info('mutableStr size width ' + attachment.size.width + ' height ' + attachment.size.height) 1474e41f4b71Sopenharmony_ci } 1475e41f4b71Sopenharmony_ci console.info('mutableStr vertical ' + attachment.verticalAlign) 1476e41f4b71Sopenharmony_ci console.info('mutableStr fit ' + attachment.objectFit) 1477e41f4b71Sopenharmony_ci if (attachment.layoutStyle !== undefined) { 1478e41f4b71Sopenharmony_ci let radius = attachment.layoutStyle.borderRadius as BorderRadiuses 1479e41f4b71Sopenharmony_ci console.info('mutableStr radius ' + JSON.stringify(radius)) 1480e41f4b71Sopenharmony_ci } 1481e41f4b71Sopenharmony_ci } 1482e41f4b71Sopenharmony_ci 1483e41f4b71Sopenharmony_ci } 1484e41f4b71Sopenharmony_ci }) 1485e41f4b71Sopenharmony_ci 1486e41f4b71Sopenharmony_ci Image(this.imagePixelMap3).width(50).height(50) 1487e41f4b71Sopenharmony_ci 1488e41f4b71Sopenharmony_ci } 1489e41f4b71Sopenharmony_ci .width('100%') 1490e41f4b71Sopenharmony_ci } 1491e41f4b71Sopenharmony_ci .height('100%') 1492e41f4b71Sopenharmony_ci } 1493e41f4b71Sopenharmony_ci} 1494e41f4b71Sopenharmony_ci``` 1495e41f4b71Sopenharmony_ci 1496e41f4b71Sopenharmony_ci 1497e41f4b71Sopenharmony_ci 1498e41f4b71Sopenharmony_ci### Example 5 1499e41f4b71Sopenharmony_ci 1500e41f4b71Sopenharmony_ciThis example shows a styled string configured with **LineHeightStyle** and **ParagraphStyle**. 1501e41f4b71Sopenharmony_ci 1502e41f4b71Sopenharmony_ci```ts 1503e41f4b71Sopenharmony_ciimport { LengthMetrics } from '@kit.ArkUI' 1504e41f4b71Sopenharmony_ciconst canvasWidth = 1000 1505e41f4b71Sopenharmony_ciconst canvasHeight = 100 1506e41f4b71Sopenharmony_ciclass LeadingMarginCreator { 1507e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 1508e41f4b71Sopenharmony_ci private offscreenCanvas: OffscreenCanvas = new OffscreenCanvas(canvasWidth, canvasHeight) 1509e41f4b71Sopenharmony_ci private offContext: OffscreenCanvasRenderingContext2D = this.offscreenCanvas.getContext("2d", this.settings) 1510e41f4b71Sopenharmony_ci public static instance: LeadingMarginCreator = new LeadingMarginCreator() 1511e41f4b71Sopenharmony_ci 1512e41f4b71Sopenharmony_ci public genSquareMark(fontSize: number): PixelMap { 1513e41f4b71Sopenharmony_ci this.offContext = this.offscreenCanvas.getContext("2d", this.settings) 1514e41f4b71Sopenharmony_ci this.clearCanvas() 1515e41f4b71Sopenharmony_ci const coordinate = fontSize * (1 - 1 / 1.5) / 2 1516e41f4b71Sopenharmony_ci const sideLength = fontSize / 1.5 1517e41f4b71Sopenharmony_ci this.offContext.fillRect(coordinate, coordinate, sideLength, sideLength) 1518e41f4b71Sopenharmony_ci return this.offContext.getPixelMap(0, 0, fontSize, fontSize) 1519e41f4b71Sopenharmony_ci } 1520e41f4b71Sopenharmony_ci 1521e41f4b71Sopenharmony_ci private clearCanvas() { 1522e41f4b71Sopenharmony_ci this.offContext.clearRect(0, 0, canvasWidth, canvasHeight) 1523e41f4b71Sopenharmony_ci } 1524e41f4b71Sopenharmony_ci} 1525e41f4b71Sopenharmony_ci@Entry 1526e41f4b71Sopenharmony_ci@Component 1527e41f4b71Sopenharmony_cistruct Index { 1528e41f4b71Sopenharmony_ci private leadingMarkCreatorInstance = LeadingMarginCreator.instance 1529e41f4b71Sopenharmony_ci leadingMarginPlaceholder1: LeadingMarginPlaceholder = { 1530e41f4b71Sopenharmony_ci pixelMap: this.leadingMarkCreatorInstance.genSquareMark(24), 1531e41f4b71Sopenharmony_ci size:[15, 15] 1532e41f4b71Sopenharmony_ci } 1533e41f4b71Sopenharmony_ci titleParagraphStyleAttr: ParagraphStyle = new ParagraphStyle({ textAlign: TextAlign.Center }); 1534e41f4b71Sopenharmony_ci // Indent the first line of the first paragraph by 15 vp. 1535e41f4b71Sopenharmony_ci paragraphStyleAttr1: ParagraphStyle = new ParagraphStyle({ textIndent: LengthMetrics.vp(15) }); 1536e41f4b71Sopenharmony_ci // Indent the second paragraph by 15 vp, with a placeholder in the first line. 1537e41f4b71Sopenharmony_ci paragraphStyleAttr2: ParagraphStyle = new ParagraphStyle({ textAlign: TextAlign.Start, leadingMargin: this.leadingMarginPlaceholder1 }); 1538e41f4b71Sopenharmony_ci // Set the maximum number of lines and text overflow mode for the third paragraph, without setting the indent. 1539e41f4b71Sopenharmony_ci paragraphStyleAttr3: ParagraphStyle = new ParagraphStyle({ textAlign: TextAlign.End, maxLines: 1, wordBreak: WordBreak.BREAK_ALL, overflow: TextOverflow.Ellipsis}); 1540e41f4b71Sopenharmony_ci // Line height style object 1541e41f4b71Sopenharmony_ci lineHeightStyle1: LineHeightStyle= new LineHeightStyle(new LengthMetrics(24)); 1542e41f4b71Sopenharmony_ci // Create a paragraph style object paragraphStyledString1. 1543e41f4b71Sopenharmony_ci paragraphStyledString1: StyledString = new StyledString("Paragraph title\nStart of the first paragraph 0123456789 End of the first paragraph\nStart of the second paragraph hello world End of the second paragraph\nThird paragraph ABCDEFGHIJKLMNOPQRSTUVWXYZ", [ 1544e41f4b71Sopenharmony_ci { 1545e41f4b71Sopenharmony_ci start: 0, 1546e41f4b71Sopenharmony_ci length: 4, 1547e41f4b71Sopenharmony_ci styledKey: StyledStringKey.PARAGRAPH_STYLE, 1548e41f4b71Sopenharmony_ci styledValue: this.titleParagraphStyleAttr 1549e41f4b71Sopenharmony_ci }, 1550e41f4b71Sopenharmony_ci { 1551e41f4b71Sopenharmony_ci start: 0, 1552e41f4b71Sopenharmony_ci length: 4, 1553e41f4b71Sopenharmony_ci styledKey: StyledStringKey.LINE_HEIGHT, 1554e41f4b71Sopenharmony_ci styledValue: new LineHeightStyle(new LengthMetrics(50)) 1555e41f4b71Sopenharmony_ci },{ 1556e41f4b71Sopenharmony_ci start: 0, 1557e41f4b71Sopenharmony_ci length: 4, 1558e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1559e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontSize: LengthMetrics.vp(24), fontWeight: FontWeight.Bolder }) 1560e41f4b71Sopenharmony_ci }, 1561e41f4b71Sopenharmony_ci { 1562e41f4b71Sopenharmony_ci start: 5, 1563e41f4b71Sopenharmony_ci length: 3, 1564e41f4b71Sopenharmony_ci styledKey: StyledStringKey.PARAGRAPH_STYLE, 1565e41f4b71Sopenharmony_ci styledValue: this.paragraphStyleAttr1 1566e41f4b71Sopenharmony_ci }, 1567e41f4b71Sopenharmony_ci { 1568e41f4b71Sopenharmony_ci start: 5, 1569e41f4b71Sopenharmony_ci length: 20, 1570e41f4b71Sopenharmony_ci styledKey: StyledStringKey.LINE_HEIGHT, 1571e41f4b71Sopenharmony_ci styledValue: this.lineHeightStyle1 1572e41f4b71Sopenharmony_ci }, 1573e41f4b71Sopenharmony_ci { 1574e41f4b71Sopenharmony_ci start: 32, 1575e41f4b71Sopenharmony_ci length: 5, 1576e41f4b71Sopenharmony_ci styledKey: StyledStringKey.PARAGRAPH_STYLE, 1577e41f4b71Sopenharmony_ci styledValue: this.paragraphStyleAttr2 1578e41f4b71Sopenharmony_ci }, 1579e41f4b71Sopenharmony_ci { 1580e41f4b71Sopenharmony_ci start: 32, 1581e41f4b71Sopenharmony_ci length: 20, 1582e41f4b71Sopenharmony_ci styledKey: StyledStringKey.LINE_HEIGHT, 1583e41f4b71Sopenharmony_ci styledValue: this.lineHeightStyle1 1584e41f4b71Sopenharmony_ci }, 1585e41f4b71Sopenharmony_ci { 1586e41f4b71Sopenharmony_ci start: 60, 1587e41f4b71Sopenharmony_ci length: 5, 1588e41f4b71Sopenharmony_ci styledKey: StyledStringKey.PARAGRAPH_STYLE, 1589e41f4b71Sopenharmony_ci styledValue: this.paragraphStyleAttr3 1590e41f4b71Sopenharmony_ci }, 1591e41f4b71Sopenharmony_ci { 1592e41f4b71Sopenharmony_ci start: 60, 1593e41f4b71Sopenharmony_ci length: 5, 1594e41f4b71Sopenharmony_ci styledKey: StyledStringKey.LINE_HEIGHT, 1595e41f4b71Sopenharmony_ci styledValue: this.lineHeightStyle1 1596e41f4b71Sopenharmony_ci } 1597e41f4b71Sopenharmony_ci ]); 1598e41f4b71Sopenharmony_ci controller: TextController = new TextController(); 1599e41f4b71Sopenharmony_ci async onPageShow() { 1600e41f4b71Sopenharmony_ci this.controller.setStyledString(this.paragraphStyledString1) 1601e41f4b71Sopenharmony_ci } 1602e41f4b71Sopenharmony_ci 1603e41f4b71Sopenharmony_ci build() { 1604e41f4b71Sopenharmony_ci Row() { 1605e41f4b71Sopenharmony_ci Column( { space : 5 }) { 1606e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller }) 1607e41f4b71Sopenharmony_ci .width(240) 1608e41f4b71Sopenharmony_ci .borderWidth(1) 1609e41f4b71Sopenharmony_ci .copyOption(CopyOptions.InApp) 1610e41f4b71Sopenharmony_ci .draggable(true) 1611e41f4b71Sopenharmony_ci 1612e41f4b71Sopenharmony_ci // Query the paragraph style. 1613e41f4b71Sopenharmony_ci Text() 1614e41f4b71Sopenharmony_ci .onClick(() => { 1615e41f4b71Sopenharmony_ci let styles = this.paragraphStyledString1.getStyles(0, this.paragraphStyledString1.length) 1616e41f4b71Sopenharmony_ci if (styles.length !== 0) { 1617e41f4b71Sopenharmony_ci for (let i = 0; i < styles.length; i++) { 1618e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 style object start:' + styles[i].start) 1619e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 style object length:' + styles[i].length) 1620e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 style object key:' + styles[i].styledKey) 1621e41f4b71Sopenharmony_ci if (styles[i].styledKey === 200) { 1622e41f4b71Sopenharmony_ci let paraAttr = styles[i].styledValue as ParagraphStyle; 1623e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 textAlign:' + paraAttr.textAlign) 1624e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 textIndent:' + paraAttr.textIndent) 1625e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 maxLines:' + paraAttr.maxLines) 1626e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 wordBreak:' + paraAttr.wordBreak) 1627e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 leadingMargin:' + paraAttr.leadingMargin) 1628e41f4b71Sopenharmony_ci console.info('paragraphStyledString1 overflow:' + paraAttr.overflow) 1629e41f4b71Sopenharmony_ci } 1630e41f4b71Sopenharmony_ci } 1631e41f4b71Sopenharmony_ci } 1632e41f4b71Sopenharmony_ci }) 1633e41f4b71Sopenharmony_ci .margin({ top: 10 }) 1634e41f4b71Sopenharmony_ci } 1635e41f4b71Sopenharmony_ci .width('100%') 1636e41f4b71Sopenharmony_ci } 1637e41f4b71Sopenharmony_ci .height('100%') 1638e41f4b71Sopenharmony_ci } 1639e41f4b71Sopenharmony_ci} 1640e41f4b71Sopenharmony_ci``` 1641e41f4b71Sopenharmony_ci 1642e41f4b71Sopenharmony_ci 1643e41f4b71Sopenharmony_ci 1644e41f4b71Sopenharmony_ci### Example 6 1645e41f4b71Sopenharmony_ci 1646e41f4b71Sopenharmony_ciThis example shows a styled string with a custom span. 1647e41f4b71Sopenharmony_ci 1648e41f4b71Sopenharmony_ci```ts 1649e41f4b71Sopenharmony_ci// xxx.ets 1650e41f4b71Sopenharmony_ciimport { drawing } from '@kit.ArkGraphics2D' 1651e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit' 1652e41f4b71Sopenharmony_ciimport { LengthMetrics } from '@kit.ArkUI' 1653e41f4b71Sopenharmony_ci 1654e41f4b71Sopenharmony_ciclass MyCustomSpan extends CustomSpan { 1655e41f4b71Sopenharmony_ci constructor(word: string, width: number, height: number) { 1656e41f4b71Sopenharmony_ci super(); 1657e41f4b71Sopenharmony_ci this.word = word; 1658e41f4b71Sopenharmony_ci this.width = width; 1659e41f4b71Sopenharmony_ci this.height = height; 1660e41f4b71Sopenharmony_ci } 1661e41f4b71Sopenharmony_ci 1662e41f4b71Sopenharmony_ci onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics { 1663e41f4b71Sopenharmony_ci return { width: this.width, height: this.height }; 1664e41f4b71Sopenharmony_ci } 1665e41f4b71Sopenharmony_ci 1666e41f4b71Sopenharmony_ci onDraw(context: DrawContext, options: CustomSpanDrawInfo) { 1667e41f4b71Sopenharmony_ci let canvas = context.canvas; 1668e41f4b71Sopenharmony_ci 1669e41f4b71Sopenharmony_ci const brush = new drawing.Brush(); 1670e41f4b71Sopenharmony_ci brush.setColor({ alpha: 255, red: 0, green: 74, blue: 175 }); 1671e41f4b71Sopenharmony_ci const font = new drawing.Font(); 1672e41f4b71Sopenharmony_ci font.setSize(25); 1673e41f4b71Sopenharmony_ci const textBlob = drawing.TextBlob.makeFromString(this.word, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); 1674e41f4b71Sopenharmony_ci canvas.attachBrush(brush); 1675e41f4b71Sopenharmony_ci canvas.drawRect({ 1676e41f4b71Sopenharmony_ci left: options.x + 10, 1677e41f4b71Sopenharmony_ci right: options.x + vp2px(this.width) - 10, 1678e41f4b71Sopenharmony_ci top: options.lineTop + 10, 1679e41f4b71Sopenharmony_ci bottom: options.lineBottom - 10 1680e41f4b71Sopenharmony_ci }); 1681e41f4b71Sopenharmony_ci 1682e41f4b71Sopenharmony_ci brush.setColor({ alpha: 255, red: 23, green: 169, blue: 141 }); 1683e41f4b71Sopenharmony_ci canvas.attachBrush(brush); 1684e41f4b71Sopenharmony_ci canvas.drawTextBlob(textBlob, options.x + 20, options.lineBottom - 15); 1685e41f4b71Sopenharmony_ci canvas.detachBrush(); 1686e41f4b71Sopenharmony_ci } 1687e41f4b71Sopenharmony_ci 1688e41f4b71Sopenharmony_ci setWord(word: string) { 1689e41f4b71Sopenharmony_ci this.word = word; 1690e41f4b71Sopenharmony_ci } 1691e41f4b71Sopenharmony_ci 1692e41f4b71Sopenharmony_ci width: number = 160; 1693e41f4b71Sopenharmony_ci word: string = "drawing"; 1694e41f4b71Sopenharmony_ci height: number = 10; 1695e41f4b71Sopenharmony_ci} 1696e41f4b71Sopenharmony_ci 1697e41f4b71Sopenharmony_ci@Entry 1698e41f4b71Sopenharmony_ci@Component 1699e41f4b71Sopenharmony_cistruct styled_string_demo6 { 1700e41f4b71Sopenharmony_ci customSpan2: MyCustomSpan = new MyCustomSpan("change", 130, 10); 1701e41f4b71Sopenharmony_ci customSpan1: MyCustomSpan = new MyCustomSpan("Hello", 80, 10); 1702e41f4b71Sopenharmony_ci customSpan3: MyCustomSpan = new MyCustomSpan("World", 80, 40); 1703e41f4b71Sopenharmony_ci style2: MutableStyledString = new MutableStyledString(this.customSpan2); 1704e41f4b71Sopenharmony_ci style1: MutableStyledString = new MutableStyledString(this.customSpan1); 1705e41f4b71Sopenharmony_ci textStyle: MutableStyledString = new MutableStyledString("123"); 1706e41f4b71Sopenharmony_ci textController: TextController = new TextController(); 1707e41f4b71Sopenharmony_ci imagePixelMap: image.PixelMap | undefined = undefined; 1708e41f4b71Sopenharmony_ci isPageShow: boolean = true; 1709e41f4b71Sopenharmony_ci 1710e41f4b71Sopenharmony_ci private async getPixmapFromMedia(resource: Resource) { 1711e41f4b71Sopenharmony_ci let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({ 1712e41f4b71Sopenharmony_ci bundleName: resource.bundleName, 1713e41f4b71Sopenharmony_ci moduleName: resource.moduleName, 1714e41f4b71Sopenharmony_ci id: resource.id 1715e41f4b71Sopenharmony_ci }); 1716e41f4b71Sopenharmony_ci let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength)); 1717e41f4b71Sopenharmony_ci let createPixelMap: image.PixelMap = await imageSource.createPixelMap({ 1718e41f4b71Sopenharmony_ci desiredPixelFormat: image.PixelMapFormat.RGBA_8888, 1719e41f4b71Sopenharmony_ci desiredSize: { width: 50, height: 50 } 1720e41f4b71Sopenharmony_ci }); 1721e41f4b71Sopenharmony_ci await imageSource.release(); 1722e41f4b71Sopenharmony_ci return createPixelMap; 1723e41f4b71Sopenharmony_ci } 1724e41f4b71Sopenharmony_ci 1725e41f4b71Sopenharmony_ci async onPageShow() { 1726e41f4b71Sopenharmony_ci if (!this.isPageShow) { 1727e41f4b71Sopenharmony_ci return 1728e41f4b71Sopenharmony_ci } 1729e41f4b71Sopenharmony_ci this.isPageShow = false; 1730e41f4b71Sopenharmony_ci 1731e41f4b71Sopenharmony_ci this.style1.appendStyledString(new MutableStyledString("Sample CustomSpan", [ 1732e41f4b71Sopenharmony_ci { 1733e41f4b71Sopenharmony_ci start: 0, 1734e41f4b71Sopenharmony_ci length: 5, 1735e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1736e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Pink }) 1737e41f4b71Sopenharmony_ci }, { 1738e41f4b71Sopenharmony_ci start: 5, 1739e41f4b71Sopenharmony_ci length: 5, 1740e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1741e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Orange, fontStyle: FontStyle.Italic }) 1742e41f4b71Sopenharmony_ci }, { 1743e41f4b71Sopenharmony_ci start: 10, 1744e41f4b71Sopenharmony_ci length: 500, 1745e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1746e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Green, fontWeight: FontWeight.Bold }) 1747e41f4b71Sopenharmony_ci } 1748e41f4b71Sopenharmony_ci ])) 1749e41f4b71Sopenharmony_ci this.style1.appendStyledString(new StyledString(this.customSpan3)) 1750e41f4b71Sopenharmony_ci this.style1.appendStyledString(new StyledString("Custom Span", [{ 1751e41f4b71Sopenharmony_ci start: 0, 1752e41f4b71Sopenharmony_ci length: 5, 1753e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 1754e41f4b71Sopenharmony_ci styledValue: new TextStyle({ fontColor: Color.Green, fontSize: LengthMetrics.px(50) }) 1755e41f4b71Sopenharmony_ci }])) 1756e41f4b71Sopenharmony_ci this.textController.setStyledString(this.style1) 1757e41f4b71Sopenharmony_ci this.imagePixelMap = await this.getPixmapFromMedia($r('sys.media.ohos_ic_public_voice')).then() 1758e41f4b71Sopenharmony_ci } 1759e41f4b71Sopenharmony_ci 1760e41f4b71Sopenharmony_ci build() { 1761e41f4b71Sopenharmony_ci Row() { 1762e41f4b71Sopenharmony_ci Column() { 1763e41f4b71Sopenharmony_ci Text(undefined, { controller: this.textController }) 1764e41f4b71Sopenharmony_ci .copyOption(CopyOptions.InApp) 1765e41f4b71Sopenharmony_ci .fontSize(30) 1766e41f4b71Sopenharmony_ci } 1767e41f4b71Sopenharmony_ci .width('100%') 1768e41f4b71Sopenharmony_ci } 1769e41f4b71Sopenharmony_ci .height('100%') 1770e41f4b71Sopenharmony_ci } 1771e41f4b71Sopenharmony_ci} 1772e41f4b71Sopenharmony_ci``` 1773e41f4b71Sopenharmony_ci 1774e41f4b71Sopenharmony_ci 1775e41f4b71Sopenharmony_ci 1776e41f4b71Sopenharmony_ci### Example 7 1777e41f4b71Sopenharmony_ci 1778e41f4b71Sopenharmony_ciThis example shows the usage of **UserDataSpan**. 1779e41f4b71Sopenharmony_ci 1780e41f4b71Sopenharmony_ci```ts 1781e41f4b71Sopenharmony_ci// xxx.ets 1782e41f4b71Sopenharmony_ciclass MyUserDateSpan extends UserDataSpan { 1783e41f4b71Sopenharmony_ci constructor(name: string, age: number) { 1784e41f4b71Sopenharmony_ci super() 1785e41f4b71Sopenharmony_ci this.name = name 1786e41f4b71Sopenharmony_ci this.age = age 1787e41f4b71Sopenharmony_ci } 1788e41f4b71Sopenharmony_ci 1789e41f4b71Sopenharmony_ci name: string 1790e41f4b71Sopenharmony_ci age: number 1791e41f4b71Sopenharmony_ci} 1792e41f4b71Sopenharmony_ci 1793e41f4b71Sopenharmony_ci@Entry 1794e41f4b71Sopenharmony_ci@Component 1795e41f4b71Sopenharmony_cistruct styled_string_demo7 { 1796e41f4b71Sopenharmony_ci @State name: string = "world" 1797e41f4b71Sopenharmony_ci @State age: number = 10 1798e41f4b71Sopenharmony_ci controller: TextController = new TextController() 1799e41f4b71Sopenharmony_ci styleString: MutableStyledString = new MutableStyledString("hello world", [{ 1800e41f4b71Sopenharmony_ci start: 0, 1801e41f4b71Sopenharmony_ci length: 11, 1802e41f4b71Sopenharmony_ci styledKey: StyledStringKey.USER_DATA, 1803e41f4b71Sopenharmony_ci styledValue: new MyUserDateSpan("hello", 21) 1804e41f4b71Sopenharmony_ci }]) 1805e41f4b71Sopenharmony_ci 1806e41f4b71Sopenharmony_ci onPageShow(): void { 1807e41f4b71Sopenharmony_ci this.controller.setStyledString(this.styleString) 1808e41f4b71Sopenharmony_ci } 1809e41f4b71Sopenharmony_ci 1810e41f4b71Sopenharmony_ci build() { 1811e41f4b71Sopenharmony_ci Column() { 1812e41f4b71Sopenharmony_ci Text(undefined, { controller: this.controller }) 1813e41f4b71Sopenharmony_ci Button("get user data").onClick(() => { 1814e41f4b71Sopenharmony_ci let arr = this.styleString.getStyles(0, this.styleString.length) 1815e41f4b71Sopenharmony_ci let userDataSpan = arr[0].styledValue as MyUserDateSpan 1816e41f4b71Sopenharmony_ci this.name = userDataSpan.name 1817e41f4b71Sopenharmony_ci this.age = userDataSpan.age 1818e41f4b71Sopenharmony_ci }) 1819e41f4b71Sopenharmony_ci Text("name:" + this.name + " age: " + this.age) 1820e41f4b71Sopenharmony_ci }.width('100%').height(250).padding({ left: 35, right: 35, top: 35 }) 1821e41f4b71Sopenharmony_ci } 1822e41f4b71Sopenharmony_ci} 1823e41f4b71Sopenharmony_ci``` 1824e41f4b71Sopenharmony_ci 1825e41f4b71Sopenharmony_ci 1826