• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Text
2
3The **\<Text>** component is used to display a piece of textual information.
4
5>  **NOTE**
6>
7>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12This component can contain the \<[Span](ts-basic-components-span.md)> and \<[ImageSpan](ts-basic-components-imagespan.md)> child components.
13
14
15## APIs
16
17Text(content?: string | Resource)
18
19Since API version 9, this API is supported in ArkTS widgets.
20
21**Parameters**
22
23| Name| Type| Mandatory| Description|
24| -------- | -------- | -------- | -------- |
25| content | string \| [Resource](ts-types.md#resource) | No| Text content. The content and style set for the **\<Text>** component do not take effect when it contains the **\<Span>** child component.<br>Default value: **' '**|
26
27## Attributes
28
29In addition to the [universal attributes](ts-universal-attributes-size.md) and [universal text attributes](ts-universal-attributes-text-style.md), the following attributes are supported.
30
31| Name                      | Type                           | Description                                              |
32| ----------------------- | ----------------------------------- | ------------------------------------------- |
33| textAlign               | [TextAlign](ts-appendix-enums.md#textalign) | Horizontal alignment mode of the text.<br>Default value: **TextAlign.Start**<br>**NOTE**<br>The text takes up the full width of the **\<Text>** component. To set vertical alignment for the text, use the [align](ts-universal-attributes-location.md) attribute. The **align** attribute alone does not control the horizontal position of the text. In other words, **Alignment.TopStart**, **Alignment.Top**, and **Alignment.TopEnd** produce the same effect, top-aligning the text; **Alignment.Start**, **Alignment.Center**, and **Alignment.End** produce the same effect, centered-aligning the text vertically; **Alignment.BottomStart**, **Alignment.Bottom**, and **Alignment.BottomEnd** produce the same effect, bottom-aligning the text. Yet, it can work with the **textAlign** attribute to jointly determine the horizontal position of the text.<br>Since API version 9, this API is supported in ArkTS widgets.|
34| textOverflow            | {overflow: [TextOverflow](ts-appendix-enums.md#textoverflow)} | Display mode when the text is too long.<br>Default value: **{overflow: TextOverflow.Clip}**<br>**NOTE**<br/>Text is clipped at the transition between words. To clip text in the middle of a word, add **\u200B** between characters.<br>If **overflow** is set to **TextOverflow.None**, **TextOverflow.Clip**, or **TextOverflow.Ellipsis**, this attribute must be used with **maxLines**. Otherwise, the setting does not take effect. **TextOverflow.None** produces the same effect as **TextOverflow.Clip**. If **overflow** is set to **TextOverflow.Marquee**, the text scrolls in a line, and neither **maxLines** nor **copyOption** takes effect.<br>Since API version 9, this API is supported in ArkTS widgets.|
35| maxLines                | number | Maximum number of lines in the text.<br>Default value: **Infinity**<br>**NOTE**<br/>By default, text is automatically folded. If this attribute is specified, the text will not exceed the specified number of lines. If there is extra text, you can use **textOverflow** to specify how it is displayed.<br>Since API version 9, this API is supported in ArkTS widgets.|
36| lineHeight              | string \| number \| [Resource](ts-types.md#resource)  | Text line height. If the value is less than or equal to **0**, the line height is not limited and the font size is adaptive. If the value of the number type, the unit fp is used.<br>Since API version 9, this API is supported in ArkTS widgets.|
37| decoration              | {<br>type: [TextDecorationType](ts-appendix-enums.md#textdecorationtype),<br>color?: [ResourceColor](ts-types.md#resourcecolor)<br>} | Style and color of the text decorative line.<br>Default value: {<br>type: TextDecorationType.None,<br>color: Color.Black<br>} <br>Since API version 9, this API is supported in ArkTS widgets.|
38| baselineOffset          | number \| string | Baseline offset of the text. The default value is **0**.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br/>If this attribute is set to a percentage, the default value is used. |
39| letterSpacing           | number \| string | Letter spacing.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br/>If this attribute is set to a percentage, the default value is used.<br> If this attribute is set to a negative value, the letters will overlap each other.|
40| minFontSize             | number \| string \| [Resource](ts-types.md#resource)      | Minimum font size.<br>For the setting to take effect, this attribute must be used together with **maxFontSize**, **maxLines**, or layout constraint settings.<br>Since API version 9, this API is supported in ArkTS widgets.                              |
41| maxFontSize             | number \| string \| [Resource](ts-types.md#resource)      | Maximum font size.<br>For the setting to take effect, this attribute must be used together with **minFontSize**, **maxLines**, or layout constraint settings.<br>Since API version 9, this API is supported in ArkTS widgets.                               |
42| textCase                | [TextCase](ts-appendix-enums.md#textcase) | Text case.<br>Default value: **TextCase.Normal**<br>Since API version 9, this API is supported in ArkTS widgets.|
43| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.<br>Default value: **CopyOptions.None**<br>This API is supported in ArkTS widgets.<br>**NOTE**<br/>When this attribute is set to **CopyOptions.InApp** or **CopyOptions.LocalDevice**, a long press on the text will display a context menu that offers the copy and select-all options. |
44| draggable  | boolean | Drag effect of the selected text.<br>This attribute cannot be used with the [onDragStart](ts-universal-events-drag-drop.md) event.<br>It must be used together with **copyOption**. When it is set to **true** and **copyOptions** is set to **CopyOptions.InApp** or **CopyOptions.LocalDevice**, the selected text can be dragged and copied to the text box.<br>Default value: **false**<br>**NOTE**<br/>This API is supported since API version 9. |
45| textShadow<sup>10+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) | Text shadow.|
46| heightAdaptivePolicy<sup>10+</sup> | [TextHeightAdaptivePolicy](ts-appendix-enums.md#textheightadaptivepolicy10) | How the adaptive height is determined for the text.<br>Default value: **TextHeightAdaptivePolicy.MAX_LINES_FIRST**<br>**NOTE**<br/>When this attribute is set to **TextHeightAdaptivePolicy.MAX_LINES_FIRST**, the **maxLines** attribute takes precedence for adjusting the text height. If the **maxLines** setting results in a layout beyond the layout constraints, the text will shrink to a font size between `minFontSize` and `maxFontSize` to allow for more content to be shown.<br>When this attribute is set to **TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST**, the **minFontSize** attribute takes precedence for adjusting the text height. If the text can fit in one line with the **minFontSize** setting, the text will enlarge to the largest possible font size between **minFontSize** and **maxFontSize**.<br>When this attribute is set to **TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST**, the layout constraints take precedence for adjusting the text height. If the resultant layout is beyond the layout constraints, the text will shrink to a font size between **minFontSize** and **maxFontSize** to respect the layout constraints. If the layout still exceeds the layout constraints after the font size is reduced to **minFontSize**, the lines that exceed the layout constraints are deleted. |
47| textIndent<sup>10+</sup> | number \| string | Indentation of the first line.<br>Default value: **0**|
48| font<sup>10+</sup> | [Font](ts-types.md#font) | Text style, covering the font size, font width, Font family, and font style.|
49
50>  **NOTE**
51>
52>  The **\<Text>** component cannot contain both the text and the child component **\<Span>** or **\<ImageSpan>**. If both of them exist, only the content in **\<Span>** or **\<ImageSpan>** is displayed.
53>
54>  For the **\<Text>** component, the default value of the [clip](ts-universal-attributes-sharp-clipping.md) attribute is **true**.
55
56## Events
57
58The [universal events](ts-universal-events-click.md) are supported.
59
60## Example
61
62### Example 1
63
64```ts
65// xxx.ets
66@Entry
67@Component
68struct TextExample1 {
69  build() {
70    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
71      // Set the horizontal alignment mode for the text.
72      // Single-line text
73      Text('textAlign').fontSize(9).fontColor(0xCCCCCC)
74      Text('TextAlign set to Center.')
75        .textAlign(TextAlign.Center)
76        .fontSize(12)
77        .border({ width: 1 })
78        .padding(10)
79        .width('100%')
80      Text('TextAlign set to Start.')
81        .textAlign(TextAlign.Start)
82        .fontSize(12)
83        .border({ width: 1 })
84        .padding(10)
85        .width('100%')
86      Text('TextAlign set to End.')
87        .textAlign(TextAlign.End)
88        .fontSize(12)
89        .border({ width: 1 })
90        .padding(10)
91        .width('100%')
92
93      // Multi-line text
94      Text('This is the text content with textAlign set to Center.')
95        .textAlign(TextAlign.Center)
96        .fontSize(12)
97        .border({ width: 1 })
98        .padding(10)
99        .width('100%')
100      Text('This is the text content with textAlign set to Start.')
101        .textAlign(TextAlign.Start)
102        .fontSize(12)
103        .border({ width: 1 })
104        .padding(10)
105        .width('100%')
106      Text('This is the text content with textAlign set to End.')
107        .textAlign(TextAlign.End)
108        .fontSize(12)
109        .border({ width: 1 })
110        .padding(10)
111        .width('100%')
112
113
114      // Set the display mode when the text is too long.
115      Text('TextOverflow+maxLines').fontSize(9).fontColor(0xCCCCCC)
116      // Clip the text when the value of maxLines is exceeded.
117      Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content. This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content.')
118        .textOverflow({ overflow: TextOverflow.Clip })
119        .maxLines(1)
120        .fontSize(12)
121        .border({ width: 1 })
122        .padding(10)
123
124      // Show an ellipsis (...) when the value of maxLines is exceeded.
125      Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('')
126        .join('\u200B'))
127        .textOverflow({ overflow: TextOverflow.Ellipsis })
128        .maxLines(1)
129        .fontSize(12)
130        .border({ width: 1 })
131        .padding(10)
132
133      Text('lineHeight').fontSize(9).fontColor(0xCCCCCC)
134      Text('This is the text with the line height set. This is the text with the line height set.')
135        .fontSize(12).border({ width: 1 }).padding(10)
136      Text('This is the text with the line height set. This is the text with the line height set.')
137        .fontSize(12).border({ width: 1 }).padding(10)
138        .lineHeight(20)
139    }.height(600).width(350).padding({ left: 35, right: 35, top: 35 })
140  }
141}
142```
143![textExp1](figures/textExp1.png)
144
145### Example 2
146
147```ts
148@Entry
149@Component
150struct TextExample2 {
151  build() {
152    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
153      Text('decoration').fontSize(9).fontColor(0xCCCCCC)
154      Text('This is the text content with the decoration set to LineThrough and the color set to Red.')
155        .decoration({
156          type: TextDecorationType.LineThrough,
157          color: Color.Red
158        })
159        .fontSize(12)
160        .border({ width: 1 })
161        .padding(10)
162        .width('100%')
163
164
165      Text('This is the text content with the decoration set to Overline and the color set to Red.')
166        .decoration({
167          type: TextDecorationType.Overline,
168          color: Color.Red
169        })
170        .fontSize(12)
171        .border({ width: 1 })
172        .padding(10)
173        .width('100%')
174
175
176      Text('This is the text content with the decoration set to Underline and the color set to Red.')
177        .decoration({
178          type: TextDecorationType.Underline,
179          color: Color.Red
180        })
181        .fontSize(12)
182        .border({ width: 1 })
183        .padding(10)
184        .width('100%')
185
186      // Set the text baseline offset.
187      Text('baselineOffset').fontSize(9).fontColor(0xCCCCCC)
188      Text('This is the text content with baselineOffset 0.')
189        .baselineOffset(0)
190        .fontSize(12)
191        .border({ width: 1 })
192        .padding(10)
193        .width('100%')
194      Text('This is the text content with baselineOffset 30.')
195        .baselineOffset(30)
196        .fontSize(12)
197        .border({ width: 1 })
198        .padding(10)
199        .width('100%')
200      Text('This is the text content with baselineOffset -20.')
201        .baselineOffset(-20)
202        .fontSize(12)
203        .border({ width: 1 })
204        .padding(10)
205        .width('100%')
206
207      // Set the letter spacing.
208      Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC)
209      Text('This is the text content with letterSpacing 0.')
210        .letterSpacing(0)
211        .fontSize(12)
212        .border({ width: 1 })
213        .padding(10)
214        .width('100%')
215      Text('This is the text content with letterSpacing 3.')
216        .letterSpacing(3)
217        .fontSize(12)
218        .border({ width: 1 })
219        .padding(10)
220        .width('100%')
221      Text('This is the text content with letterSpacing -1.')
222        .letterSpacing(-1)
223        .fontSize(12)
224        .border({ width: 1 })
225        .padding(10)
226        .width('100%')
227
228      Text('textCase').fontSize(9).fontColor(0xCCCCCC)
229      Text('This is the text content with textCase set to Normal.')
230        .textCase(TextCase.Normal)
231        .fontSize(12)
232        .border({ width: 1 })
233        .padding(10)
234        .width('100%')
235      // Display the text in lowercase.
236      Text('This is the text content with textCase set to LowerCase.')
237        .textCase(TextCase.LowerCase)
238        .fontSize(12)
239        .border({ width: 1 })
240        .padding(10)
241        .width('100%')
242      // Display the text in uppercase.
243      Text('This is the text content with textCase set to UpperCase.')
244        .textCase(TextCase.UpperCase)
245        .fontSize(12).border({ width: 1 }).padding(10)
246
247    }.height(700).width(350).padding({ left: 35, right: 35, top: 35 })
248  }
249}
250```
251![textExp1](figures/textExp2.png)
252
253### Example 3
254
255```ts
256@Entry
257@Component
258struct TextExample {
259  build() {
260    Column({ space: 8 }) {
261      // Set the text shadow.
262      Text('textShadow').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%')
263      Text('textShadow')
264        .width('80%')
265        .height(55)
266        .fontSize(40)
267        .lineHeight(55)
268        .textAlign(TextAlign.Center)
269        .textShadow({ radius: 10, color: Color.Black, offsetX: 0, offsetY: 0 })
270        .borderWidth(1)
271      Divider()
272      // Set how the adaptive height is determined for the text.
273      Text('heightAdaptivePolicy').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%')
274      Text('This is the text with the height adaptive policy set')
275        .width('80%')
276        .height(90)
277        .borderWidth(1)
278        .minFontSize(10)
279        .maxFontSize(30)
280        .maxLines(3)
281        .textOverflow({ overflow: TextOverflow.Ellipsis })
282        .heightAdaptivePolicy(TextHeightAdaptivePolicy.MAX_LINES_FIRST)
283      Text('This is the text with the height adaptive policy set')
284        .width('80%')
285        .height(90)
286        .borderWidth(1)
287        .minFontSize(10)
288        .maxFontSize(30)
289        .maxLines(3)
290        .textOverflow({ overflow: TextOverflow.Ellipsis })
291        .heightAdaptivePolicy(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST)
292      Text('This is the text with the height adaptive policy set')
293        .width('80%')
294        .height(90)
295        .borderWidth(1)
296        .minFontSize(10)
297        .maxFontSize(30)
298        .maxLines(3)
299        .textOverflow({ overflow: TextOverflow.Ellipsis })
300        .heightAdaptivePolicy(TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST)
301      Divider()
302      // Set the text to continuously scroll when text overflow occurs.
303      Text('marquee').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%')
304      Text('This is the text with the text overflow set marquee')
305        .width(300)
306        .borderWidth(1)
307        .textOverflow({ overflow: TextOverflow.MARQUEE })
308    }
309  }
310}
311```
312
313![](figures/text_3.gif)
314