1e41f4b71Sopenharmony_ci# Styled String (StyledString/MutableStyledString)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciStyled strings, implemented by **StyledString** or **MutableStyledString** (collectively referred to as **StyledString**, with **MutableStyledString** inheriting from **StyledString**), are powerful markup objects that can be used to set text styles at the character or paragraph level. By attaching a **StyledString** to a text component, you can modify the text in various ways, including changing the font size, adding font colors, making the text clickable, and customizing the drawing of text, among others. For details, see [Styled String](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md).
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciStyled strings provide a variety of style objects that cover various common text formatting styles. You can also create **CustomSpan** objects to apply custom styles.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Creating and Applying a StyledString Object
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci  You can attach a **StyledString** object to a text component using the **setStyledString(StyledString)** API provided by **TextController**. You are advised to call the API in the **onPageShow** callback for immediate display of the styled string text content. Avoid calling it in **aboutToAppear**, as the component may have not yet been mounted to the node tree at the time **aboutToAppear** is executed, preventing the styled string text content from appearing upon page load.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci  ```ts
12e41f4b71Sopenharmony_ci  @Entry
13e41f4b71Sopenharmony_ci  @Component
14e41f4b71Sopenharmony_ci  struct styled_string_demo1 {
15e41f4b71Sopenharmony_ci    styledString1: StyledString = new StyledString("45-minute workout");
16e41f4b71Sopenharmony_ci    mutableStyledString1: MutableStyledString = new MutableStyledString("35-minute workout");
17e41f4b71Sopenharmony_ci    controller1: TextController = new TextController();
18e41f4b71Sopenharmony_ci    controller2: TextController = new TextController();
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci    async onPageShow() {
21e41f4b71Sopenharmony_ci      this.controller1.setStyledString(this.styledString1)
22e41f4b71Sopenharmony_ci      this.controller2.setStyledString(this.mutableStyledString1)
23e41f4b71Sopenharmony_ci    }
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci    build() {
26e41f4b71Sopenharmony_ci      Column() {
27e41f4b71Sopenharmony_ci        // Display the styled string.
28e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller1 })
29e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller2 })
30e41f4b71Sopenharmony_ci      }
31e41f4b71Sopenharmony_ci      .width('100%')
32e41f4b71Sopenharmony_ci    }
33e41f4b71Sopenharmony_ci  }
34e41f4b71Sopenharmony_ci  ```
35e41f4b71Sopenharmony_ci  ![StyledString_Init](figures/span_string_init.png)
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci## Setting the Text Style
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciStyled strings offer multiple style objects, such as [TextStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#textstyle), [TextShadowStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#textshadowstyle), [DecorationStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#decorationstyle), [BaselineOffsetStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#baselineoffsetstyle), [LineHeightStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#lineheightstyle), and [LetterSpacingStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#letterspacingstyle), for setting text styles.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci- Creating and applying a **TextStyle** object
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci  ```ts
44e41f4b71Sopenharmony_ci  import { LengthMetrics } from '@kit.ArkUI'
45e41f4b71Sopenharmony_ci  @Entry
46e41f4b71Sopenharmony_ci  @Component
47e41f4b71Sopenharmony_ci  struct styled_string_demo2 {
48e41f4b71Sopenharmony_ci    textStyleAttrs: TextStyle = new TextStyle({ fontWeight: FontWeight.Bolder, fontSize: LengthMetrics.vp(24), fontStyle: FontStyle.Italic })
49e41f4b71Sopenharmony_ci    mutableStyledString: MutableStyledString = new MutableStyledString("35-minute workout goal achieved", [
50e41f4b71Sopenharmony_ci      {
51e41f4b71Sopenharmony_ci        start: 2,
52e41f4b71Sopenharmony_ci        length: 2,
53e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
54e41f4b71Sopenharmony_ci        styledValue: this.textStyleAttrs
55e41f4b71Sopenharmony_ci      },
56e41f4b71Sopenharmony_ci      {
57e41f4b71Sopenharmony_ci        start: 7,
58e41f4b71Sopenharmony_ci        length: 4,
59e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
60e41f4b71Sopenharmony_ci        styledValue: new TextStyle({ fontColor: Color.Orange, fontSize: LengthMetrics.vp(12)})
61e41f4b71Sopenharmony_ci      }
62e41f4b71Sopenharmony_ci    ]);
63e41f4b71Sopenharmony_ci    controller: TextController = new TextController();
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci    async onPageShow() {
66e41f4b71Sopenharmony_ci      this.controller.setStyledString(this.mutableStyledString)
67e41f4b71Sopenharmony_ci    }
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci    build() {
70e41f4b71Sopenharmony_ci      Column() {
71e41f4b71Sopenharmony_ci        // Display the styled string.
72e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller })
73e41f4b71Sopenharmony_ci          .margin({ top: 10 })
74e41f4b71Sopenharmony_ci      }
75e41f4b71Sopenharmony_ci      .width('100%')
76e41f4b71Sopenharmony_ci    }
77e41f4b71Sopenharmony_ci  }
78e41f4b71Sopenharmony_ci  ```
79e41f4b71Sopenharmony_ci  ![StyledString_TextStyle](figures/StyledString_TextStyle.png)
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci- Creating and applying a **TextShadow** object
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci  ```ts
84e41f4b71Sopenharmony_ci  // xxx.ets
85e41f4b71Sopenharmony_ci  @Entry
86e41f4b71Sopenharmony_ci  @Component
87e41f4b71Sopenharmony_ci  struct styled_string_demo3 {
88e41f4b71Sopenharmony_ci    mutableStyledString: MutableStyledString = new MutableStyledString("35-minute workout", [
89e41f4b71Sopenharmony_ci    {
90e41f4b71Sopenharmony_ci      start: 0,
91e41f4b71Sopenharmony_ci      length: 3,
92e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.TEXT_SHADOW,
93e41f4b71Sopenharmony_ci      styledValue: new TextShadowStyle({
94e41f4b71Sopenharmony_ci        radius: 5,
95e41f4b71Sopenharmony_ci        type: ShadowType.COLOR,
96e41f4b71Sopenharmony_ci        color: Color.Red,
97e41f4b71Sopenharmony_ci        offsetX: 10,
98e41f4b71Sopenharmony_ci        offsetY: 10
99e41f4b71Sopenharmony_ci      })
100e41f4b71Sopenharmony_ci    }
101e41f4b71Sopenharmony_ci    ]);
102e41f4b71Sopenharmony_ci    controller: TextController = new TextController();
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci    async onPageShow() {
105e41f4b71Sopenharmony_ci      this.controller.setStyledString(this.mutableStyledString)
106e41f4b71Sopenharmony_ci    }
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci    build() {
109e41f4b71Sopenharmony_ci      Column() {
110e41f4b71Sopenharmony_ci        // Display the styled string.
111e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller })
112e41f4b71Sopenharmony_ci      }
113e41f4b71Sopenharmony_ci      .width('100%')
114e41f4b71Sopenharmony_ci    }
115e41f4b71Sopenharmony_ci  }
116e41f4b71Sopenharmony_ci  ```
117e41f4b71Sopenharmony_ci  ![StyledString_TextShadow](figures/styled_string_text_shadow.png)
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci- Creating and applying a **Text DecorationStyle** object
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci  ```ts
122e41f4b71Sopenharmony_ci  // xxx.ets
123e41f4b71Sopenharmony_ci  @Entry
124e41f4b71Sopenharmony_ci  @Component
125e41f4b71Sopenharmony_ci  struct styled_string_demo4 {
126e41f4b71Sopenharmony_ci    mutableStyledString: MutableStyledString = new MutableStyledString("35-minute workout", [
127e41f4b71Sopenharmony_ci    {
128e41f4b71Sopenharmony_ci      start: 0,
129e41f4b71Sopenharmony_ci      length: 3,
130e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.DECORATION,
131e41f4b71Sopenharmony_ci      styledValue: new DecorationStyle({type: TextDecorationType.LineThrough, color: Color.Red})
132e41f4b71Sopenharmony_ci    }
133e41f4b71Sopenharmony_ci    ]);
134e41f4b71Sopenharmony_ci    controller: TextController = new TextController();
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci    async onPageShow() {
137e41f4b71Sopenharmony_ci      this.controller.setStyledString(this.mutableStyledString)
138e41f4b71Sopenharmony_ci    }
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci    build() {
141e41f4b71Sopenharmony_ci      Column() {
142e41f4b71Sopenharmony_ci        // Display the styled string.
143e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller })
144e41f4b71Sopenharmony_ci      }
145e41f4b71Sopenharmony_ci      .width('100%')
146e41f4b71Sopenharmony_ci    }
147e41f4b71Sopenharmony_ci  }
148e41f4b71Sopenharmony_ci  ```
149e41f4b71Sopenharmony_ci  ![StyledString_Decoration](figures/styled_string_decoration.png)
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci- Creating and applying a **Text BaselineOffsetStyle** object
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci  ```ts
154e41f4b71Sopenharmony_ci  import { LengthMetrics, LengthUnit } from '@kit.ArkUI'
155e41f4b71Sopenharmony_ci  // xxx.ets
156e41f4b71Sopenharmony_ci  @Entry
157e41f4b71Sopenharmony_ci  @Component
158e41f4b71Sopenharmony_ci  struct styled_string_demo5 {
159e41f4b71Sopenharmony_ci    mutableStyledString: MutableStyledString = new MutableStyledString("35-minute workout", [
160e41f4b71Sopenharmony_ci      {
161e41f4b71Sopenharmony_ci        start: 0,
162e41f4b71Sopenharmony_ci        length: 3,
163e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.BASELINE_OFFSET,
164e41f4b71Sopenharmony_ci        styledValue: new BaselineOffsetStyle(LengthMetrics.px(20))
165e41f4b71Sopenharmony_ci      }
166e41f4b71Sopenharmony_ci    ]);
167e41f4b71Sopenharmony_ci    controller: TextController = new TextController();
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci    async onPageShow() {
170e41f4b71Sopenharmony_ci      this.controller.setStyledString(this.mutableStyledString)
171e41f4b71Sopenharmony_ci    }
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci    build() {
174e41f4b71Sopenharmony_ci      Column() {
175e41f4b71Sopenharmony_ci        // Display the styled string.
176e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller })
177e41f4b71Sopenharmony_ci      }
178e41f4b71Sopenharmony_ci      .width('100%')
179e41f4b71Sopenharmony_ci    }
180e41f4b71Sopenharmony_ci  }
181e41f4b71Sopenharmony_ci  ```
182e41f4b71Sopenharmony_ci  ![StyledString_Baseline](figures/styled_string_baselineoffset.png)
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci- Creating and applying a **LineHeightStyle** object
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci  ```ts
187e41f4b71Sopenharmony_ci  import { LengthMetrics, LengthUnit } from '@kit.ArkUI'
188e41f4b71Sopenharmony_ci  // xxx.ets
189e41f4b71Sopenharmony_ci  @Entry
190e41f4b71Sopenharmony_ci  @Component
191e41f4b71Sopenharmony_ci  struct styled_string_demo6 {
192e41f4b71Sopenharmony_ci    mutableStyledString: MutableStyledString = new MutableStyledString("35-minute workout\nFighting\nAchieved", [
193e41f4b71Sopenharmony_ci      {
194e41f4b71Sopenharmony_ci        start: 8,
195e41f4b71Sopenharmony_ci        length: 3,
196e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.LINE_HEIGHT,
197e41f4b71Sopenharmony_ci        styledValue: new LineHeightStyle(LengthMetrics.vp(50))
198e41f4b71Sopenharmony_ci      }
199e41f4b71Sopenharmony_ci    ]);
200e41f4b71Sopenharmony_ci    controller: TextController = new TextController();
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci    async onPageShow() {
203e41f4b71Sopenharmony_ci      this.controller.setStyledString(this.mutableStyledString)
204e41f4b71Sopenharmony_ci    }
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci    build() {
207e41f4b71Sopenharmony_ci      Column() {
208e41f4b71Sopenharmony_ci        // Display the styled string.
209e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller })
210e41f4b71Sopenharmony_ci      }
211e41f4b71Sopenharmony_ci      .width('100%')
212e41f4b71Sopenharmony_ci      .margin({ top: 10 })
213e41f4b71Sopenharmony_ci    }
214e41f4b71Sopenharmony_ci  }
215e41f4b71Sopenharmony_ci  ```
216e41f4b71Sopenharmony_ci  ![StyledString_lineHeight](figures/styled_string_lineHeight.png)
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci- Creating and applying a **LetterSpacingStyle** object
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci  ```ts
221e41f4b71Sopenharmony_ci  import { LengthMetrics, LengthUnit } from '@kit.ArkUI'
222e41f4b71Sopenharmony_ci  // xxx.ets
223e41f4b71Sopenharmony_ci  @Entry
224e41f4b71Sopenharmony_ci  @Component
225e41f4b71Sopenharmony_ci  struct styled_string_demo7 {
226e41f4b71Sopenharmony_ci    mutableStyledString: MutableStyledString = new MutableStyledString("35-minute workout", [
227e41f4b71Sopenharmony_ci      {
228e41f4b71Sopenharmony_ci        start: 0,
229e41f4b71Sopenharmony_ci        length: 2,
230e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.LETTER_SPACING,
231e41f4b71Sopenharmony_ci        styledValue: new LetterSpacingStyle(new LengthMetrics(20, LengthUnit.VP))
232e41f4b71Sopenharmony_ci      }
233e41f4b71Sopenharmony_ci    ]);
234e41f4b71Sopenharmony_ci    controller: TextController = new TextController();
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci    async onPageShow() {
237e41f4b71Sopenharmony_ci      this.controller.setStyledString(this.mutableStyledString)
238e41f4b71Sopenharmony_ci    }
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci    build() {
241e41f4b71Sopenharmony_ci      Column() {
242e41f4b71Sopenharmony_ci        // Display the styled string.
243e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller })
244e41f4b71Sopenharmony_ci      }
245e41f4b71Sopenharmony_ci      .width('100%')
246e41f4b71Sopenharmony_ci    }
247e41f4b71Sopenharmony_ci  }
248e41f4b71Sopenharmony_ci  ```
249e41f4b71Sopenharmony_ci  ![StyledString_letterSpacing](figures/styled_string_letterspacing.png)
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ci## Setting the Paragraph Style
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ciYou can set the paragraph style using [ParagraphStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#paragraphstyle). The figure below shows how to divide paragraphs in the text, with each paragraph ending with a newline character \n.
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci![paragraphs](figures/styledstringParagraphs.png)
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ciThe following example shows how to create and apply a paragraph style. The style is applied to the start, end or any position within a paragraph; it does not apply to non-paragraph areas.
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci  ```ts
260e41f4b71Sopenharmony_ci  import { LengthMetrics } from '@kit.ArkUI'
261e41f4b71Sopenharmony_ci  titleParagraphStyleAttr: ParagraphStyle = new ParagraphStyle({ textAlign: TextAlign.Center });
262e41f4b71Sopenharmony_ci  // Create a paragraph style for a 15 vp first-line text indent.
263e41f4b71Sopenharmony_ci  paragraphStyleAttr1: ParagraphStyle = new ParagraphStyle({ textIndent: LengthMetrics.vp(15) });
264e41f4b71Sopenharmony_ci  // Create a paragraph style object paragraphStyledString1.
265e41f4b71Sopenharmony_ci  paragraphStyledString1: MutableStyledString = new MutableStyledString("Paragraph Title\nFirst paragraph starts 0123456789 First paragraph ends.", [
266e41f4b71Sopenharmony_ci    {
267e41f4b71Sopenharmony_ci      start: 0,
268e41f4b71Sopenharmony_ci      length: 4,
269e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.PARAGRAPH_STYLE,
270e41f4b71Sopenharmony_ci      styledValue: this.titleParagraphStyleAttr
271e41f4b71Sopenharmony_ci    },
272e41f4b71Sopenharmony_ci    {
273e41f4b71Sopenharmony_ci      start: 0,
274e41f4b71Sopenharmony_ci      length: 4,
275e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.LINE_HEIGHT,
276e41f4b71Sopenharmony_ci      styledValue: new LineHeightStyle(new LengthMetrics(50))
277e41f4b71Sopenharmony_ci    },{
278e41f4b71Sopenharmony_ci    start: 0,
279e41f4b71Sopenharmony_ci    length: 4,
280e41f4b71Sopenharmony_ci    styledKey: StyledStringKey.FONT,
281e41f4b71Sopenharmony_ci    styledValue: new TextStyle({ fontSize: LengthMetrics.vp(24), fontWeight: FontWeight.Bolder })
282e41f4b71Sopenharmony_ci  },
283e41f4b71Sopenharmony_ci    {
284e41f4b71Sopenharmony_ci      start: 5,
285e41f4b71Sopenharmony_ci      length: 3,
286e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.PARAGRAPH_STYLE,
287e41f4b71Sopenharmony_ci      styledValue: this.paragraphStyleAttr1
288e41f4b71Sopenharmony_ci    },
289e41f4b71Sopenharmony_ci    {
290e41f4b71Sopenharmony_ci      start: 5,
291e41f4b71Sopenharmony_ci      length: 20,
292e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.LINE_HEIGHT,
293e41f4b71Sopenharmony_ci      styledValue: this.lineHeightStyle1
294e41f4b71Sopenharmony_ci    }
295e41f4b71Sopenharmony_ci  ]);
296e41f4b71Sopenharmony_ci  ```
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_ci  In addition to presetting styles when creating a styled string, you can also clear the original styles and replace them with new ones later using the [replaceStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#replacestyle) API. After the replacement, you need to proactively trigger an update to the bound styled string on the attached text component's controller.
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci  ```ts
301e41f4b71Sopenharmony_ci  import { LengthMetrics } from '@kit.ArkUI'
302e41f4b71Sopenharmony_ci  // Set the maximum number of lines and text overflow mode for the paragraph, without setting the indent.
303e41f4b71Sopenharmony_ci  paragraphStyleAttr3: ParagraphStyle = new ParagraphStyle({ textAlign: TextAlign.End, maxLines: 1, wordBreak: WordBreak.BREAK_ALL, overflow: TextOverflow.Ellipsis});
304e41f4b71Sopenharmony_ci  // Later in the code, trigger an update to the paragraph style.
305e41f4b71Sopenharmony_ci  controller: TextController = new TextController();
306e41f4b71Sopenharmony_ci  this.paragraphStyledString1.replaceStyle({
307e41f4b71Sopenharmony_ci    start: 5,
308e41f4b71Sopenharmony_ci    length: 3,
309e41f4b71Sopenharmony_ci    styledKey: StyledStringKey.PARAGRAPH_STYLE,
310e41f4b71Sopenharmony_ci    styledValue: this.paragraphStyleAttr3
311e41f4b71Sopenharmony_ci  })
312e41f4b71Sopenharmony_ci  this.controller.setStyledString(this.mutableStyledString3)
313e41f4b71Sopenharmony_ci  ```
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci## Using Images
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ciYou can add images using [ImageAttachment](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#imageattachment).
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ciThe following example shows how to attach images and text to the same **MutableStyledString** object for mixed display of text and images.
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci  ```ts
322e41f4b71Sopenharmony_ci  // xxx.ets
323e41f4b71Sopenharmony_ci  import { image } from '@kit.ImageKit'
324e41f4b71Sopenharmony_ci  import { LengthMetrics } from '@kit.ArkUI'
325e41f4b71Sopenharmony_ci
326e41f4b71Sopenharmony_ci  @Entry
327e41f4b71Sopenharmony_ci  @Component
328e41f4b71Sopenharmony_ci  struct styled_string_demo4 {
329e41f4b71Sopenharmony_ci    @State message: string = 'Hello World'
330e41f4b71Sopenharmony_ci    imagePixelMap: image.PixelMap | undefined = undefined
331e41f4b71Sopenharmony_ci    @State imagePixelMap3: image.PixelMap | undefined = undefined
332e41f4b71Sopenharmony_ci    mutableStr: MutableStyledString = new MutableStyledString('123');
333e41f4b71Sopenharmony_ci    controller: TextController = new TextController();
334e41f4b71Sopenharmony_ci    mutableStr2: MutableStyledString = new MutableStyledString('This is set decoration line style to the mutableStr2', [{
335e41f4b71Sopenharmony_ci      start: 0,
336e41f4b71Sopenharmony_ci      length: 15,
337e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.DECORATION,
338e41f4b71Sopenharmony_ci      styledValue: new DecorationStyle({
339e41f4b71Sopenharmony_ci        type: TextDecorationType.Overline,
340e41f4b71Sopenharmony_ci        color: Color.Orange,
341e41f4b71Sopenharmony_ci        style: TextDecorationStyle.DOUBLE
342e41f4b71Sopenharmony_ci      })
343e41f4b71Sopenharmony_ci    }])
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci    async aboutToAppear() {
346e41f4b71Sopenharmony_ci      console.info("aboutToAppear initial imagePixelMap")
347e41f4b71Sopenharmony_ci      this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.sea'))
348e41f4b71Sopenharmony_ci    }
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ci    private async getPixmapFromMedia(resource: Resource) {
351e41f4b71Sopenharmony_ci      let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({
352e41f4b71Sopenharmony_ci        bundleName: resource.bundleName,
353e41f4b71Sopenharmony_ci        moduleName: resource.moduleName,
354e41f4b71Sopenharmony_ci        id: resource.id
355e41f4b71Sopenharmony_ci      })
356e41f4b71Sopenharmony_ci      let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength))
357e41f4b71Sopenharmony_ci      let createPixelMap: image.PixelMap = await imageSource.createPixelMap({
358e41f4b71Sopenharmony_ci        desiredPixelFormat: image.PixelMapFormat.RGBA_8888
359e41f4b71Sopenharmony_ci      })
360e41f4b71Sopenharmony_ci      await imageSource.release()
361e41f4b71Sopenharmony_ci      return createPixelMap
362e41f4b71Sopenharmony_ci    }
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci    leadingMarginValue: ParagraphStyle = new ParagraphStyle({ leadingMargin: LengthMetrics.vp(5)})
365e41f4b71Sopenharmony_ci    // Line height style object
366e41f4b71Sopenharmony_ci    lineHeightStyle1: LineHeightStyle= new LineHeightStyle(new LengthMetrics(24));
367e41f4b71Sopenharmony_ci    //Bold style
368e41f4b71Sopenharmony_ci    boldTextStyle: TextStyle = new TextStyle({ fontWeight: FontWeight.Bold });
369e41f4b71Sopenharmony_ci    // Create a paragraph style object paragraphStyledString1.
370e41f4b71Sopenharmony_ci    paragraphStyledString1: MutableStyledString = new MutableStyledString("\n30 HD prints\nCYN5.15 off Limited offer", [
371e41f4b71Sopenharmony_ci      {
372e41f4b71Sopenharmony_ci        start: 0,
373e41f4b71Sopenharmony_ci        length: 28,
374e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.PARAGRAPH_STYLE,
375e41f4b71Sopenharmony_ci        styledValue: this.leadingMarginValue
376e41f4b71Sopenharmony_ci      },
377e41f4b71Sopenharmony_ci      {
378e41f4b71Sopenharmony_ci        start: 14,
379e41f4b71Sopenharmony_ci        length: 9,
380e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
381e41f4b71Sopenharmony_ci        styledValue: new TextStyle({ fontSize: LengthMetrics.vp(14), fontColor: '#B22222' })
382e41f4b71Sopenharmony_ci      },
383e41f4b71Sopenharmony_ci      {
384e41f4b71Sopenharmony_ci        start: 24,
385e41f4b71Sopenharmony_ci        length: 4,
386e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
387e41f4b71Sopenharmony_ci        styledValue: new TextStyle({ fontSize: LengthMetrics.vp(14), fontWeight: FontWeight.Lighter })
388e41f4b71Sopenharmony_ci      },
389e41f4b71Sopenharmony_ci      {
390e41f4b71Sopenharmony_ci        start: 11,
391e41f4b71Sopenharmony_ci        length: 4,
392e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.LINE_HEIGHT,
393e41f4b71Sopenharmony_ci        styledValue: this.lineHeightStyle1
394e41f4b71Sopenharmony_ci      }
395e41f4b71Sopenharmony_ci    ]);
396e41f4b71Sopenharmony_ci    paragraphStyledString2: MutableStyledString = new MutableStyledString("\n¥16.21 3000+ reviews", [
397e41f4b71Sopenharmony_ci      {
398e41f4b71Sopenharmony_ci        start: 0,
399e41f4b71Sopenharmony_ci        length: 5,
400e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.PARAGRAPH_STYLE,
401e41f4b71Sopenharmony_ci        styledValue: this.leadingMarginValue
402e41f4b71Sopenharmony_ci      },
403e41f4b71Sopenharmony_ci      {
404e41f4b71Sopenharmony_ci        start: 0,
405e41f4b71Sopenharmony_ci        length: 4,
406e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.LINE_HEIGHT,
407e41f4b71Sopenharmony_ci        styledValue: new LineHeightStyle(new LengthMetrics(60))
408e41f4b71Sopenharmony_ci      },
409e41f4b71Sopenharmony_ci      {
410e41f4b71Sopenharmony_ci        start: 0,
411e41f4b71Sopenharmony_ci        length: 7,
412e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
413e41f4b71Sopenharmony_ci        styledValue: this.boldTextStyle
414e41f4b71Sopenharmony_ci      },
415e41f4b71Sopenharmony_ci      {
416e41f4b71Sopenharmony_ci        start: 1,
417e41f4b71Sopenharmony_ci        length: 1,
418e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
419e41f4b71Sopenharmony_ci        styledValue: new TextStyle({ fontSize: LengthMetrics.vp(18) })
420e41f4b71Sopenharmony_ci      },
421e41f4b71Sopenharmony_ci      {
422e41f4b71Sopenharmony_ci        start: 2,
423e41f4b71Sopenharmony_ci        length: 2,
424e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
425e41f4b71Sopenharmony_ci        styledValue: new TextStyle({ fontSize: LengthMetrics.vp(36) })
426e41f4b71Sopenharmony_ci      },
427e41f4b71Sopenharmony_ci      {
428e41f4b71Sopenharmony_ci        start: 4,
429e41f4b71Sopenharmony_ci        length: 3,
430e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
431e41f4b71Sopenharmony_ci        styledValue: new TextStyle({ fontSize: LengthMetrics.vp(20) })
432e41f4b71Sopenharmony_ci      },
433e41f4b71Sopenharmony_ci      {
434e41f4b71Sopenharmony_ci        start: 7,
435e41f4b71Sopenharmony_ci        length: 9,
436e41f4b71Sopenharmony_ci        styledKey: StyledStringKey.FONT,
437e41f4b71Sopenharmony_ci        styledValue: new TextStyle({ fontColor: Color.Grey, fontSize: LengthMetrics.vp(14)})
438e41f4b71Sopenharmony_ci      }
439e41f4b71Sopenharmony_ci    ])
440e41f4b71Sopenharmony_ci
441e41f4b71Sopenharmony_ci    build() {
442e41f4b71Sopenharmony_ci      Row() {
443e41f4b71Sopenharmony_ci        Column({ space: 10 }) {
444e41f4b71Sopenharmony_ci          Text(undefined, { controller: this.controller })
445e41f4b71Sopenharmony_ci            .copyOption(CopyOptions.InApp)
446e41f4b71Sopenharmony_ci            .draggable(true)
447e41f4b71Sopenharmony_ci            .backgroundColor('#FFFFFF')
448e41f4b71Sopenharmony_ci            .borderRadius(5)
449e41f4b71Sopenharmony_ci
450e41f4b71Sopenharmony_ci          Button('View Product Details')
451e41f4b71Sopenharmony_ci            .onClick(() => {
452e41f4b71Sopenharmony_ci              if (this.imagePixelMap !== undefined) {
453e41f4b71Sopenharmony_ci                this.mutableStr = new MutableStyledString(new ImageAttachment({
454e41f4b71Sopenharmony_ci                  value: this.imagePixelMap,
455e41f4b71Sopenharmony_ci                  size: { width: 180, height: 160 },
456e41f4b71Sopenharmony_ci                  verticalAlign: ImageSpanAlignment.BASELINE,
457e41f4b71Sopenharmony_ci                  objectFit: ImageFit.Fill
458e41f4b71Sopenharmony_ci                }))
459e41f4b71Sopenharmony_ci                this.paragraphStyledString1.appendStyledString(this.paragraphStyledString2)
460e41f4b71Sopenharmony_ci                this.mutableStr.appendStyledString(this.paragraphStyledString1)
461e41f4b71Sopenharmony_ci                this.controller.setStyledString(this.mutableStr)
462e41f4b71Sopenharmony_ci              }
463e41f4b71Sopenharmony_ci            })
464e41f4b71Sopenharmony_ci        }
465e41f4b71Sopenharmony_ci        .width('100%')
466e41f4b71Sopenharmony_ci      }
467e41f4b71Sopenharmony_ci      .height('100%')
468e41f4b71Sopenharmony_ci      .backgroundColor('#F8F8FF')
469e41f4b71Sopenharmony_ci    }
470e41f4b71Sopenharmony_ci  }
471e41f4b71Sopenharmony_ci  ```
472e41f4b71Sopenharmony_ci  ![StyledString_ImageAttachment](figures/StyledStringImageAttachment.png)
473e41f4b71Sopenharmony_ci
474e41f4b71Sopenharmony_ci## Setting Events
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_ciYou can use [GestureStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#gesturestyle) to set up **onClick** and **onLongPress** events to enable text to respond to click and long-press actions.
477e41f4b71Sopenharmony_ci
478e41f4b71Sopenharmony_ciIn addition to initializing styled strings with initial style objects, you can also use the [setStyle](../reference/apis-arkui/arkui-ts/ts-universal-styled-string.md#setstyle) API to overlay new styles or update existing ones. After making changes, you need to manually trigger an update of the bound styled string on the attached text component's controller. 
479e41f4b71Sopenharmony_ci
480e41f4b71Sopenharmony_ci  ```ts
481e41f4b71Sopenharmony_ciimport { drawing } from '@kit.ArkGraphics2D';
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ciclass MyCustomSpan extends CustomSpan {
484e41f4b71Sopenharmony_ci  constructor(word: string, width: number, height: number, fontSize: number) {
485e41f4b71Sopenharmony_ci    super();
486e41f4b71Sopenharmony_ci    this.word = word;
487e41f4b71Sopenharmony_ci    this.width = width;
488e41f4b71Sopenharmony_ci    this.height = height;
489e41f4b71Sopenharmony_ci    this.fontSize = fontSize;
490e41f4b71Sopenharmony_ci  }
491e41f4b71Sopenharmony_ci
492e41f4b71Sopenharmony_ci  onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics {
493e41f4b71Sopenharmony_ci    return { width: this.width, height: this.height }
494e41f4b71Sopenharmony_ci  }
495e41f4b71Sopenharmony_ci
496e41f4b71Sopenharmony_ci  onDraw(context: DrawContext, options: CustomSpanDrawInfo) {
497e41f4b71Sopenharmony_ci    let canvas = context.canvas;
498e41f4b71Sopenharmony_ci
499e41f4b71Sopenharmony_ci    const brush = new drawing.Brush();
500e41f4b71Sopenharmony_ci    brush.setColor({ alpha: 255, red: 0, green: 0, blue: 0 })
501e41f4b71Sopenharmony_ci    const font = new drawing.Font()
502e41f4b71Sopenharmony_ci    font.setSize(vp2px(this.fontSize))
503e41f4b71Sopenharmony_ci    const textBlob = drawing.TextBlob.makeFromString(this.word.substring(0, 5), font, drawing.TextEncoding.TEXT_ENCODING_UTF8)
504e41f4b71Sopenharmony_ci    canvas.attachBrush(brush)
505e41f4b71Sopenharmony_ci
506e41f4b71Sopenharmony_ci    this.onDrawRectByRadius(context, options.x, options.x + vp2px(this.width), options.lineTop, options.lineBottom, 20)
507e41f4b71Sopenharmony_ci    brush.setColor({ alpha: 255, red: 255, green: 255, blue: 255 })
508e41f4b71Sopenharmony_ci    canvas.attachBrush(brush)
509e41f4b71Sopenharmony_ci    canvas.drawTextBlob(textBlob, options.x, options.lineBottom - 30)
510e41f4b71Sopenharmony_ci    brush.setColor({ alpha: 255, red: 255, green: 228 , blue: 196 })
511e41f4b71Sopenharmony_ci    canvas.attachBrush(brush)
512e41f4b71Sopenharmony_ci    const textBlob1 = drawing.TextBlob.makeFromString(this.word.substring(5), font, drawing.TextEncoding.TEXT_ENCODING_UTF8)
513e41f4b71Sopenharmony_ci    canvas.drawTextBlob(textBlob1, options.x + vp2px(100), options.lineBottom - 30)
514e41f4b71Sopenharmony_ci
515e41f4b71Sopenharmony_ci    canvas.detachBrush()
516e41f4b71Sopenharmony_ci  }
517e41f4b71Sopenharmony_ci  onDrawRectByRadius(context: DrawContext, left: number, right: number, top: number, bottom: number, radius: number) {
518e41f4b71Sopenharmony_ci    let canvas = context.canvas
519e41f4b71Sopenharmony_ci    let path = new drawing.Path()
520e41f4b71Sopenharmony_ci
521e41f4b71Sopenharmony_ci    // Draw a rectangle with rounded corners.
522e41f4b71Sopenharmony_ci    path.moveTo(left  + radius, top)
523e41f4b71Sopenharmony_ci    path.lineTo(right - radius, top)
524e41f4b71Sopenharmony_ci    path.arcTo(right - 2 * radius, top, right, top + 2 * radius, 270, 90)
525e41f4b71Sopenharmony_ci    path.lineTo(right, bottom - radius)
526e41f4b71Sopenharmony_ci    path.arcTo(right - 2 * radius, bottom - 2 * radius, right, bottom, 0, 90)
527e41f4b71Sopenharmony_ci
528e41f4b71Sopenharmony_ci    path.lineTo(left + 2 * radius, bottom)
529e41f4b71Sopenharmony_ci    path.arcTo(left, bottom - 2 * radius, left + 2 * radius, bottom, 90, 90)
530e41f4b71Sopenharmony_ci    path.lineTo(left, top + 2 * radius)
531e41f4b71Sopenharmony_ci    path.arcTo(left, top, left + 2 * radius, top + 2 * radius, 180, 90)
532e41f4b71Sopenharmony_ci
533e41f4b71Sopenharmony_ci    canvas.drawPath(path)
534e41f4b71Sopenharmony_ci  }
535e41f4b71Sopenharmony_ci  setWord(word: string) {
536e41f4b71Sopenharmony_ci    this.word = word;
537e41f4b71Sopenharmony_ci  }
538e41f4b71Sopenharmony_ci
539e41f4b71Sopenharmony_ci  width: number = 160
540e41f4b71Sopenharmony_ci  word: string = "drawing"
541e41f4b71Sopenharmony_ci  height: number = 10
542e41f4b71Sopenharmony_ci  fontSize: number = 16
543e41f4b71Sopenharmony_ci}
544e41f4b71Sopenharmony_ci
545e41f4b71Sopenharmony_ci@Entry
546e41f4b71Sopenharmony_ci@Component
547e41f4b71Sopenharmony_cistruct styled_string_demo6 {
548e41f4b71Sopenharmony_ci  customSpan3: MyCustomSpan = new MyCustomSpan("99VIP88%off", 200, 40, 30)
549e41f4b71Sopenharmony_ci  textStyle: MutableStyledString = new MutableStyledString("123");
550e41f4b71Sopenharmony_ci  textController: TextController = new TextController()
551e41f4b71Sopenharmony_ci  isPageShow: boolean = true
552e41f4b71Sopenharmony_ci
553e41f4b71Sopenharmony_ci  async onPageShow() {
554e41f4b71Sopenharmony_ci    if (!this.isPageShow) {
555e41f4b71Sopenharmony_ci      return
556e41f4b71Sopenharmony_ci    }
557e41f4b71Sopenharmony_ci    this.isPageShow = false
558e41f4b71Sopenharmony_ci    this.textController.setStyledString(new StyledString(this.customSpan3))
559e41f4b71Sopenharmony_ci  }
560e41f4b71Sopenharmony_ci
561e41f4b71Sopenharmony_ci  build() {
562e41f4b71Sopenharmony_ci    Row() {
563e41f4b71Sopenharmony_ci      Column() {
564e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.textController })
565e41f4b71Sopenharmony_ci          .copyOption(CopyOptions.InApp)
566e41f4b71Sopenharmony_ci          .fontSize(30)
567e41f4b71Sopenharmony_ci      }
568e41f4b71Sopenharmony_ci      .width('100%')
569e41f4b71Sopenharmony_ci    }
570e41f4b71Sopenharmony_ci    .height('100%')
571e41f4b71Sopenharmony_ci  }
572e41f4b71Sopenharmony_ci}
573e41f4b71Sopenharmony_ci  ```
574e41f4b71Sopenharmony_ci![CustomSpanDemo](figures/StyledString_CustomSpan_Scene.PNG)
575e41f4b71Sopenharmony_ci
576e41f4b71Sopenharmony_ci## Example
577e41f4b71Sopenharmony_ci
578e41f4b71Sopenharmony_ci```ts
579e41f4b71Sopenharmony_ciimport { LengthMetrics } from '@kit.ArkUI';
580e41f4b71Sopenharmony_ci
581e41f4b71Sopenharmony_ci@Entry
582e41f4b71Sopenharmony_ci@Component
583e41f4b71Sopenharmony_cistruct Index {
584e41f4b71Sopenharmony_ci  alignCenterParagraphStyleAttr: ParagraphStyle = new ParagraphStyle({ textAlign: TextAlign.Center });
585e41f4b71Sopenharmony_ci  // Line height style object
586e41f4b71Sopenharmony_ci  lineHeightStyle1: LineHeightStyle= new LineHeightStyle(LengthMetrics.vp(24));
587e41f4b71Sopenharmony_ci  //Bold style
588e41f4b71Sopenharmony_ci  boldTextStyle: TextStyle = new TextStyle({ fontWeight: FontWeight.Bold });
589e41f4b71Sopenharmony_ci  // Create a paragraph style object paragraphStyledString1.
590e41f4b71Sopenharmony_ci  paragraphStyledString1: MutableStyledString = new MutableStyledString("Diamond Membership expired\nRenew to keep your perks ", [
591e41f4b71Sopenharmony_ci    {
592e41f4b71Sopenharmony_ci      start: 0,
593e41f4b71Sopenharmony_ci      length: 4,
594e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.PARAGRAPH_STYLE,
595e41f4b71Sopenharmony_ci      styledValue: this.alignCenterParagraphStyleAttr
596e41f4b71Sopenharmony_ci    },
597e41f4b71Sopenharmony_ci    {
598e41f4b71Sopenharmony_ci      start: 0,
599e41f4b71Sopenharmony_ci      length: 4,
600e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.LINE_HEIGHT,
601e41f4b71Sopenharmony_ci      styledValue: new LineHeightStyle(LengthMetrics.vp(40))
602e41f4b71Sopenharmony_ci    },
603e41f4b71Sopenharmony_ci    {
604e41f4b71Sopenharmony_ci      start: 11,
605e41f4b71Sopenharmony_ci      length: 14,
606e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.FONT,
607e41f4b71Sopenharmony_ci      styledValue: new TextStyle({ fontSize: LengthMetrics.vp(14), fontColor: Color.Grey })
608e41f4b71Sopenharmony_ci    },
609e41f4b71Sopenharmony_ci    {
610e41f4b71Sopenharmony_ci      start: 11,
611e41f4b71Sopenharmony_ci      length: 4,
612e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.PARAGRAPH_STYLE,
613e41f4b71Sopenharmony_ci      styledValue: this.alignCenterParagraphStyleAttr
614e41f4b71Sopenharmony_ci    },
615e41f4b71Sopenharmony_ci    {
616e41f4b71Sopenharmony_ci      start: 11,
617e41f4b71Sopenharmony_ci      length: 4,
618e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.LINE_HEIGHT,
619e41f4b71Sopenharmony_ci      styledValue: this.lineHeightStyle1
620e41f4b71Sopenharmony_ci    }
621e41f4b71Sopenharmony_ci  ]);
622e41f4b71Sopenharmony_ci  paragraphStyledString2: MutableStyledString = new MutableStyledString("\n¥4.88¥15", [
623e41f4b71Sopenharmony_ci    {
624e41f4b71Sopenharmony_ci      start: 0,
625e41f4b71Sopenharmony_ci      length: 4,
626e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.PARAGRAPH_STYLE,
627e41f4b71Sopenharmony_ci      styledValue: this.alignCenterParagraphStyleAttr
628e41f4b71Sopenharmony_ci    },
629e41f4b71Sopenharmony_ci    {
630e41f4b71Sopenharmony_ci      start: 0,
631e41f4b71Sopenharmony_ci      length: 4,
632e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.LINE_HEIGHT,
633e41f4b71Sopenharmony_ci      styledValue: new LineHeightStyle(LengthMetrics.vp(60))
634e41f4b71Sopenharmony_ci    },
635e41f4b71Sopenharmony_ci    {
636e41f4b71Sopenharmony_ci      start: 0,
637e41f4b71Sopenharmony_ci      length: 6,
638e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.FONT,
639e41f4b71Sopenharmony_ci      styledValue: this.boldTextStyle
640e41f4b71Sopenharmony_ci    },
641e41f4b71Sopenharmony_ci    {
642e41f4b71Sopenharmony_ci      start: 1,
643e41f4b71Sopenharmony_ci      length: 1,
644e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.FONT,
645e41f4b71Sopenharmony_ci      styledValue: new TextStyle({ fontSize: LengthMetrics.vp(18)})
646e41f4b71Sopenharmony_ci    },
647e41f4b71Sopenharmony_ci    {
648e41f4b71Sopenharmony_ci      start: 2,
649e41f4b71Sopenharmony_ci      length: 4,
650e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.FONT,
651e41f4b71Sopenharmony_ci      styledValue: new TextStyle({ fontSize: LengthMetrics.vp(40)})
652e41f4b71Sopenharmony_ci    },
653e41f4b71Sopenharmony_ci    {
654e41f4b71Sopenharmony_ci      start: 6,
655e41f4b71Sopenharmony_ci      length: 3,
656e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.FONT,
657e41f4b71Sopenharmony_ci      styledValue: new TextStyle({ fontColor: Color.Grey, fontSize: LengthMetrics.vp(14)})
658e41f4b71Sopenharmony_ci    },
659e41f4b71Sopenharmony_ci    {
660e41f4b71Sopenharmony_ci      start: 6,
661e41f4b71Sopenharmony_ci      length: 3,
662e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.DECORATION,
663e41f4b71Sopenharmony_ci      styledValue: new DecorationStyle({ type: TextDecorationType.LineThrough, color: Color.Grey })
664e41f4b71Sopenharmony_ci    }
665e41f4b71Sopenharmony_ci  ])
666e41f4b71Sopenharmony_ci  paragraphStyledString3: MutableStyledString = new MutableStyledString("\nOffer ends in 02:06", [
667e41f4b71Sopenharmony_ci    {
668e41f4b71Sopenharmony_ci      start: 0,
669e41f4b71Sopenharmony_ci      length: 4,
670e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.PARAGRAPH_STYLE,
671e41f4b71Sopenharmony_ci      styledValue: this.alignCenterParagraphStyleAttr
672e41f4b71Sopenharmony_ci    },
673e41f4b71Sopenharmony_ci    {
674e41f4b71Sopenharmony_ci      start: 0,
675e41f4b71Sopenharmony_ci      length: 4,
676e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.LINE_HEIGHT,
677e41f4b71Sopenharmony_ci      styledValue: new LineHeightStyle(LengthMetrics.vp(30))
678e41f4b71Sopenharmony_ci    },
679e41f4b71Sopenharmony_ci    {
680e41f4b71Sopenharmony_ci      start: 1,
681e41f4b71Sopenharmony_ci      length: 2,
682e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.FONT,
683e41f4b71Sopenharmony_ci      styledValue: new TextStyle({ fontColor: '#FFD700', fontWeight: FontWeight.Bold })
684e41f4b71Sopenharmony_ci    },
685e41f4b71Sopenharmony_ci    {
686e41f4b71Sopenharmony_ci      start: 4,
687e41f4b71Sopenharmony_ci      length: 2,
688e41f4b71Sopenharmony_ci      styledKey: StyledStringKey.FONT,
689e41f4b71Sopenharmony_ci      styledValue: new TextStyle({ fontColor: '#FFD700', fontWeight: FontWeight.Bold })
690e41f4b71Sopenharmony_ci    }
691e41f4b71Sopenharmony_ci  ])
692e41f4b71Sopenharmony_ci  controller: TextController = new TextController();
693e41f4b71Sopenharmony_ci
694e41f4b71Sopenharmony_ci  build() {
695e41f4b71Sopenharmony_ci    Row() {
696e41f4b71Sopenharmony_ci      Column( { space : 5 }) {
697e41f4b71Sopenharmony_ci        Text(undefined, { controller: this.controller })
698e41f4b71Sopenharmony_ci          .width(240)
699e41f4b71Sopenharmony_ci          .copyOption(CopyOptions.InApp)
700e41f4b71Sopenharmony_ci          .draggable(true)
701e41f4b71Sopenharmony_ci          .onAppear(()=>{
702e41f4b71Sopenharmony_ci            this.paragraphStyledString2.appendStyledString(this.paragraphStyledString3)
703e41f4b71Sopenharmony_ci            this.paragraphStyledString1.appendStyledString(this.paragraphStyledString2)
704e41f4b71Sopenharmony_ci            this.controller.setStyledString(this.paragraphStyledString1)
705e41f4b71Sopenharmony_ci          })
706e41f4b71Sopenharmony_ci
707e41f4b71Sopenharmony_ci        Button("Renew")
708e41f4b71Sopenharmony_ci          .width(200)
709e41f4b71Sopenharmony_ci          .fontColor(Color.White)
710e41f4b71Sopenharmony_ci          .fontSize(18)
711e41f4b71Sopenharmony_ci          .backgroundColor('#3CB371')
712e41f4b71Sopenharmony_ci          .margin({ bottom: 10 })
713e41f4b71Sopenharmony_ci      }
714e41f4b71Sopenharmony_ci      .borderWidth(1).borderColor('#FFDEAD')
715e41f4b71Sopenharmony_ci      .margin({ left: 10 })
716e41f4b71Sopenharmony_ci    }
717e41f4b71Sopenharmony_ci    .height('60%')
718e41f4b71Sopenharmony_ci  }
719e41f4b71Sopenharmony_ci}
720e41f4b71Sopenharmony_ci```
721e41f4b71Sopenharmony_ci![StyledString_SceneDemo](figures/styledString_sceneDemo.png)
722