• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Text Display
2
3
4The **\<Text>** component is used to display a piece of textual information. For details, see [Text](../reference/arkui-ts/ts-basic-components-text.md).
5
6
7## Creating Text
8
9You can create text in either of the following ways:
10
11
12- Entering strings
13
14  ```ts
15  Text ('I am a piece of text')
16  ```
17
18
19![en-us_image_0000001563060685](figures/en-us_image_0000001563060685.png)
20
21
22- Referencing Resource objects
23  You can use **$r** to create a **Resource** object to reference resources in **/resources/base/element/string.json**.
24
25
26  ```ts
27  Text($r('app.string.module_desc'))
28    .baselineOffset(0)
29    .fontSize(30)
30    .border({ width: 1 })
31    .padding(10)
32    .width(300)
33  ```
34
35  ![en-us_image_0000001511580872](figures/en-us_image_0000001511580872.png)
36
37
38## Adding Child Components
39
40The **\<Text>** component accepts \<[Span](../reference/arkui-ts/ts-basic-components-span.md)> as its child component. You can add one or more **\<Span>** child components to a **\<Text>** component to display a piece of information, such as the product description and statement of commitment.
41
42- Creating a \<Span> Component
43  The **\<Span>** component works only when included in a **\<Text>** component. If both the **\<Span>** and **\<Text>** components have text configured, the text of the **\<Span>** overwrites that of the **\<Text>** component.
44
45
46  ```ts
47  Text (' I'm Text') {
48    Span (' I'm Span')
49  }
50  .padding(10)
51  .borderWidth(1)
52  ```
53
54  ![en-us_image_0000001562700441](figures/en-us_image_0000001562700441.png)
55
56- Set the text decorative line.
57  Use the **decoration** attribute to set the style and color of the text decorative line.
58
59
60  ```ts
61  Text() {
62    Span('I'm Span1,') .fontSize (16).fontColor (Color.Grey)
63      .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
64    Span('I'm Span2').fontColor (Color.Blue).fontSize (16)
65      .fontStyle(FontStyle.Italic)
66      .decoration({ type: TextDecorationType.Underline, color: Color.Black })
67    Span('I'm Span3').fontSize(16).fontColor(Color.Grey)
68      .decoration({ type: TextDecorationType.Overline, color: Color.Green })
69  }
70  .borderWidth(1)
71  .padding(10)
72  ```
73
74  ![en-us_image_0000001562700437](figures/en-us_image_0000001562700437.png)
75
76- Use the **textCase** attribute to set the text case.
77
78  ```ts
79  Text() {
80    Span('I am Upper-span').fontSize(12)
81      .textCase(TextCase.UpperCase)
82  }
83  .borderWidth(1)
84  .padding(10)
85  ```
86
87  ![en-us_image_0000001562940525](figures/en-us_image_0000001562940525.png)
88
89- Adding Events
90  The **\<Span>** component does not have size information. Therefore, only the **onClick** event is supported.
91
92
93  ```ts
94  Text() {
95    Span('I am Upper-span').fontSize(12)
96      .textCase(TextCase.UpperCase)
97      .onClick(()=>{
98        console.info (' I'm Span - onClick')
99      })
100  }
101  ```
102
103
104## Setting Styles
105
106- Use the **textAlign** attribute to set the alignment mode of text.
107
108  ```ts
109  Text('Left aligned')
110    .width(300)
111    .textAlign(TextAlign.Start)
112    .border({ width: 1 })
113    .padding(10)
114  Text ('Center aligned')
115    .width(300)
116    .textAlign(TextAlign.Center)
117    .border({ width: 1 })
118    .padding(10)
119  Text('Right aligned')
120    .width(300)
121    .textAlign(TextAlign.End)
122    .border({ width: 1 })
123    .padding(10)
124  ```
125
126  ![en-us_image_0000001511421260](figures/en-us_image_0000001511421260.png)
127
128- Use the **textOverflow** attribute to set the display mode for when the text is too long. This attribute must be used together with **maxLines**. By default, the text is automatically folded.
129
130  ```ts
131  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.')
132    .width(250)
133    .textOverflow({ overflow: TextOverflow.None })
134    .maxLines(1)
135    .fontSize(12)
136    .border({ width: 1 }).padding(10)
137  Text('I am extra long text, with an ellipse displayed for any excess.')
138    .width(250)
139    .textOverflow({ overflow: TextOverflow.Ellipsis })
140    .maxLines(1)
141    .fontSize(12)
142    .border({ width: 1 }).padding(10)
143  ```
144
145  ![en-us_image_0000001563060693](figures/en-us_image_0000001563060693.png)
146
147  ![en-us_image_0000001563060701](figures/en-us_image_0000001563060701.png)
148
149- Use the **lineHeight** attribute to set the text line height.
150
151  ```ts
152  Text('This is the text with the line height set. This is the text with the line height set.')
153    .width(300).fontSize(12).border({ width: 1 }).padding(10)
154  Text('This is the text with the line height set. This is the text with the line height set.')
155    .width(300).fontSize(12).border({ width: 1 }).padding(10)
156    .lineHeight(20)
157  ```
158
159  ![en-us_image_0000001511740480](figures/en-us_image_0000001511740480.png)
160
161- Use the **decoration** attribute to set the style and color of the text decorative line.
162
163  ```ts
164  Text('This is the text')
165    .decoration({
166      type: TextDecorationType.LineThrough,
167      color: Color.Red
168    })
169    .borderWidth(1).padding(10).margin(5)
170  Text('This is the text')
171    .decoration({
172      type: TextDecorationType.Overline,
173      color: Color.Red
174    })
175    .borderWidth(1).padding(10).margin(5)
176  Text('This is the text')
177    .decoration({
178      type: TextDecorationType.Underline,
179      color: Color.Red
180    })
181    .borderWidth(1).padding(10).margin(5)
182  ```
183
184  ![en-us_image_0000001511580888](figures/en-us_image_0000001511580888.png)
185
186- Use the **baselineOffset** attribute to set the baseline offset of the text.
187
188  ```ts
189  Text('This is the text content with baselineOffset 0.')
190    .baselineOffset(0)
191    .fontSize(12)
192    .border({ width: 1 })
193    .padding(10)
194    .width('100%')
195    .margin(5)
196  Text('This is the text content with baselineOffset 30.')
197    .baselineOffset(30)
198    .fontSize(12)
199    .border({ width: 1 })
200    .padding(10)
201    .width('100%')
202    .margin(5)
203
204  Text('This is the text content with baselineOffset -20.')
205    .baselineOffset(-20)
206    .fontSize(12)
207    .border({ width: 1 })
208    .padding(10)
209    .width('100%')
210    .margin(5)
211  ```
212
213  ![en-us_image_0000001562820789](figures/en-us_image_0000001562820789.png)
214
215- Use the **letterSpacing** attribute to set the letter spacing.
216
217  ```ts
218  Text('This is the text content with letterSpacing 0.')
219    .letterSpacing(0)
220    .fontSize(12)
221    .border({ width: 1 })
222    .padding(10)
223    .width('100%')
224    .margin(5)
225  Text('This is the text content with letterSpacing 3.')
226    .letterSpacing(3)
227    .fontSize(12)
228    .border({ width: 1 })
229    .padding(10)
230    .width('100%')
231    .margin(5)
232  Text('This is the text content with letterSpacing -1.')
233    .letterSpacing(-1)
234    .fontSize(12)
235    .border({ width: 1 })
236    .padding(10)
237    .width('100%')
238    .margin(5)
239  ```
240
241  ![en-us_image_0000001562940513](figures/en-us_image_0000001562940513.png)
242
243- Use the **minFontSize** and **maxFontSize** attributes to set the minimum and maximum font size, respectively. For the settings to take effect, these attributes must be used together with **maxLines** or layout constraint settings.
244
245  ```ts
246  Text('My maximum font size is 30, minimum font size is 5, width is 250, and maximum number of lines is 1')
247    .width(250)
248    .maxLines(1)
249    .maxFontSize(30)
250    .minFontSize(5)
251    .border({ width: 1 })
252    .padding(10)
253    .margin(5)
254  Text('My maximum font size is 30, minimum font size is 5, width is 250, and maximum number of lines is 2')
255    .width(250)
256    .maxLines(2)
257    .maxFontSize(30)
258    .minFontSize(5)
259    .border({ width: 1 })
260    .padding(10)
261    .margin(5)
262  Text('My maximum font size is 30, minimum font size is 15, width is 250, and line height is 50')
263    .width(250)
264    .height(50)
265    .maxFontSize(30)
266    .minFontSize(15)
267    .border({ width: 1 })
268    .padding(10)
269    .margin(5)
270  Text('My maximum font size is 30, minimum font size is 15, width is 250, and line height is 100')
271    .width(250)
272    .height(100)
273    .maxFontSize(30)
274    .minFontSize(15)
275    .border({ width: 1 })
276    .padding(10)
277    .margin(5)
278  ```
279
280  ![en-us_image_0000001511740472](figures/en-us_image_0000001511740472.png)
281
282- Use the **textCase** attribute to set the text case.
283
284  ```ts
285  Text('This is the text content with textCase set to Normal.')
286    .textCase(TextCase.Normal)
287    .padding(10)
288    .border({ width: 1 })
289    .padding(10)
290    .margin(5)
291
292  // The text is displayed in lowercase.
293  Text('This is the text content with textCase set to LowerCase.')
294    .textCase(TextCase.LowerCase)
295    .border({ width: 1 })
296    .padding(10)
297    .margin(5)
298
299  // The text is displayed in uppercase.
300  Text('This is the text content with textCase set to UpperCase.')
301    .textCase(TextCase.UpperCase)
302    .border({ width: 1 })
303    .padding(10)
304    .margin(5)
305  ```
306
307  ![en-us_image_0000001562940529](figures/en-us_image_0000001562940529.png)
308
309- Use the **copyOption** attribute to set whether copy and paste is allowed.
310
311  ```ts
312  Text("This text is copyable")
313    .fontSize(30)
314    .copyOption(CopyOptions.InApp)
315  ```
316
317  ![en-us_image_0000001511580868](figures/en-us_image_0000001511580868.png)
318
319
320## Adding Events
321
322The **\<Text>** component supports the [universal events](../reference/arkui-ts/ts-universal-events-click.md). It can be bound to the **onClick**, **onTouch**, or other events to respond to user operations.
323
324
325```ts
326Text ('Click Me')
327  .onClick(()=>{
328      console.info('I am the response to the click event');
329   })
330```
331
332
333## Example Scenario
334
335
336```ts
337// xxx.ets
338@Entry
339@Component
340struct TextExample {
341  build() {
342    Column() {
343      Row() {
344        Text("1").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
345        Text("I am entry 1")
346          .fontSize(12)
347          .fontColor(Color.Blue)
348          .maxLines(1)
349          .textOverflow({ overflow: TextOverflow.Ellipsis })
350          .fontWeight(300)
351        Text ("Top Hit")
352          .margin({ left: 6 })
353          .textAlign(TextAlign.Center)
354          .fontSize(10)
355          .fontColor(Color.White)
356          .fontWeight(600)
357          .backgroundColor(0x770100)
358          .borderRadius(5)
359          .width(15)
360          .height(14)
361      }.width('100%').margin(5)
362
363      Row() {
364        Text("2").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
365        Text("I am entry 2")
366          .fontSize(12)
367          .fontColor(Color.Blue)
368          .fontWeight(300)
369          .constraintSize({ maxWidth: 200 })
370          .maxLines(1)
371          .textOverflow({ overflow: TextOverflow.Ellipsis })
372        Text ("Hot")
373          .margin({ left: 6 })
374          .textAlign(TextAlign.Center)
375          .fontSize(10)
376          .fontColor(Color.White)
377          .fontWeight(600)
378          .backgroundColor(0xCC5500)
379          .borderRadius(5)
380          .width(15)
381          .height(14)
382      }.width('100%').margin(5)
383
384      Row() {
385        Text("3").fontSize(14).fontColor(Color.Orange).margin({ left: 10, right: 10 })
386        Text("I am entry 3")
387          .fontSize(12)
388          .fontColor(Color.Blue)
389          .fontWeight(300)
390          .maxLines(1)
391          .constraintSize({ maxWidth: 200 })
392          .textOverflow({ overflow: TextOverflow.Ellipsis })
393        Text ("Hot")
394          .margin({ left: 6 })
395          .textAlign(TextAlign.Center)
396          .fontSize(10)
397          .fontColor(Color.White)
398          .fontWeight(600)
399          .backgroundColor(0xCC5500)
400          .borderRadius(5)
401          .width(15)
402          .height(14)
403      }.width('100%').margin(5)
404
405      Row() {
406        Text("4").fontSize(14).fontColor(Color.Grey).margin({ left: 10, right: 10 })
407        Text("I am entry 4")
408          .fontSize(12)
409          .fontColor(Color.Blue)
410          .fontWeight(300)
411          .constraintSize({ maxWidth: 200 })
412          .maxLines(1)
413          .textOverflow({ overflow: TextOverflow.Ellipsis })
414      }.width('100%').margin(5)
415    }.width('100%')
416  }
417}
418
419```
420
421![en-us_image_0000001562820805](figures/en-us_image_0000001562820805.png)
422