1# 文本显示 (Text/Span)
2
3
4Text是文本组件,通常用于展示用户视图,如显示文章的文字内容。Span则用于呈现显示行内文本。
5
6具体用法请参考[Text](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md)和[Span](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md)组件的使用说明。
7
8
9## 创建文本
10
11Text可通过以下两种方式来创建:
12
13
14- string字符串。
15
16  ```ts
17  Text('我是一段文本')
18  ```
19
20
21![zh-cn_image_0000001563060685](figures/zh-cn_image_0000001563060685.png)
22
23
24- 引用Resource资源。
25
26  资源引用类型可以通过$r创建Resource类型对象,文件位置为/resources/base/element/string.json27
28
29  ```ts
30  Text($r('app.string.module_desc'))
31    .baselineOffset(0)
32    .fontSize(30)
33    .border({ width: 1 })
34    .padding(10)
35    .width(300)
36  ```
37
38  ![zh-cn_image_0000001511580872](figures/zh-cn_image_0000001511580872.png)
39
40
41## 添加子组件
42
43[Span](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md)只能作为[Text](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md)和[RichEditor](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md)组件的子组件显示文本内容。可以在一个Text内添加多个Span来显示一段信息,例如产品说明书、承诺书等。
44
45- 创建Span。
46
47  Span组件必须嵌入在Text组件中才能显示,单独的Span组件不会呈现任何内容。Text与Span同时配置文本内容时,Span内容覆盖Text内容。
48
49
50  ```ts
51  Text('我是Text') {
52    Span('我是Span')
53  }
54  .padding(10)
55  .borderWidth(1)
56  ```
57
58  ![zh-cn_image_0000001562700441](figures/zh-cn_image_0000001562700441.png)
59
60- 设置文本装饰线及颜色。
61
62  通过[decoration](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md#decoration)设置文本装饰线及颜色。
63
64
65  ```ts
66  Text() {
67    Span('我是Span1,').fontSize(16).fontColor(Color.Grey)
68      .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
69    Span('我是Span2').fontColor(Color.Blue).fontSize(16)
70      .fontStyle(FontStyle.Italic)
71      .decoration({ type: TextDecorationType.Underline, color: Color.Black })
72    Span(',我是Span3').fontSize(16).fontColor(Color.Grey)
73      .decoration({ type: TextDecorationType.Overline, color: Color.Green })
74  }
75  .borderWidth(1)
76  .padding(10)
77  ```
78
79  ![zh-cn_image_0000001562700437](figures/zh-cn_image_0000001562700437.png)
80
81- 通过[textCase](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md#textcase)设置文字一直保持大写或者小写状态。
82
83  ```ts
84  Text() {
85    Span('I am Upper-span').fontSize(12)
86      .textCase(TextCase.UpperCase)
87  }
88  .borderWidth(1)
89  .padding(10)
90  ```
91
92  ![zh-cn_image_0000001562940525](figures/zh-cn_image_0000001562940525.png)
93
94- 添加事件。
95
96  由于Span组件无尺寸信息,事件仅支持添加点击事件[onClick](../reference/apis-arkui/arkui-ts/ts-universal-events-click.md#onclick)。
97
98
99  ```ts
100  Text() {
101    Span('I am Upper-span').fontSize(12)
102      .textCase(TextCase.UpperCase)
103      .onClick(() => {
104        console.info('我是Span——onClick');
105      })
106  }
107  ```
108
109
110## 自定义文本样式
111
112- 通过[textAlign](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#textalign)属性设置文本对齐样式。
113
114  ```ts
115  Text('左对齐')
116    .width(300)
117    .textAlign(TextAlign.Start)
118    .border({ width: 1 })
119    .padding(10)
120  Text('中间对齐')
121    .width(300)
122    .textAlign(TextAlign.Center)
123    .border({ width: 1 })
124    .padding(10)
125  Text('右对齐')
126    .width(300)
127    .textAlign(TextAlign.End)
128    .border({ width: 1 })
129    .padding(10)
130  ```
131
132  ![zh-cn_image_0000001511421260](figures/zh-cn_image_0000001511421260.png)
133
134- 通过[textOverflow](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#textoverflow)属性控制文本超长处理,textOverflow需配合[maxLines](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#maxlines)一起使用(默认情况下文本自动折行)。
135
136  ```ts
137  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.')
138    .width(250)
139    .textOverflow({ overflow: TextOverflow.None })
140    .maxLines(1)
141    .fontSize(12)
142    .border({ width: 1 })
143    .padding(10)
144  Text('我是超长文本,超出的部分显示省略号。I am an extra long text, with ellipses displayed for any excess。')
145    .width(250)
146    .textOverflow({ overflow: TextOverflow.Ellipsis })
147    .maxLines(1)
148    .fontSize(12)
149    .border({ width: 1 })
150    .padding(10)
151  Text('当文本溢出其尺寸时,文本将滚动显示。When the text overflows its dimensions, the text will scroll for displaying.')
152    .width(250)
153    .textOverflow({ overflow: TextOverflow.MARQUEE })
154    .maxLines(1)
155    .fontSize(12)
156    .border({ width: 1 })
157    .padding(10)
158  ```
159
160  ![zh-cn_image_0000001563060701](figures/zh-cn_image_0000001563060701.gif)
161
162- 通过[lineHeight](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#lineheight)属性设置文本行高。
163
164  ```ts
165  Text('This is the text with the line height set. This is the text with the line height set.')
166    .width(300).fontSize(12).border({ width: 1 }).padding(10)
167  Text('This is the text with the line height set. This is the text with the line height set.')
168    .width(300).fontSize(12).border({ width: 1 }).padding(10)
169    .lineHeight(20)
170  ```
171
172  ![zh-cn_image_0000001511740480](figures/zh-cn_image_0000001511740480.png)
173
174- 通过[decoration](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#decoration)属性设置文本装饰线样式及其颜色。
175
176  ```ts
177  Text('This is the text')
178    .decoration({
179      type: TextDecorationType.LineThrough,
180      color: Color.Red
181    })
182    .borderWidth(1).padding(10).margin(5)
183  Text('This is the text')
184    .decoration({
185      type: TextDecorationType.Overline,
186      color: Color.Red
187    })
188    .borderWidth(1).padding(10).margin(5)
189  Text('This is the text')
190    .decoration({
191      type: TextDecorationType.Underline,
192      color: Color.Red
193    })
194    .borderWidth(1).padding(10).margin(5)
195  ```
196
197  ![zh-cn_image_0000001511580888](figures/zh-cn_image_0000001511580888.png)
198
199- 通过[baselineOffset](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#baselineoffset)属性设置文本基线的偏移量。
200
201  ```ts
202  Text('This is the text content with baselineOffset 0.')
203    .baselineOffset(0)
204    .fontSize(12)
205    .border({ width: 1 })
206    .padding(10)
207    .width('100%')
208    .margin(5)
209  Text('This is the text content with baselineOffset 30.')
210    .baselineOffset(30)
211    .fontSize(12)
212    .border({ width: 1 })
213    .padding(10)
214    .width('100%')
215    .margin(5)
216  Text('This is the text content with baselineOffset -20.')
217    .baselineOffset(-20)
218    .fontSize(12)
219    .border({ width: 1 })
220    .padding(10)
221    .width('100%')
222    .margin(5)
223  ```
224
225  ![zh-cn_image_0000001562820789](figures/zh-cn_image_0000001562820789.png)
226
227- 通过[letterSpacing](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#letterspacing)属性设置文本字符间距。
228
229  ```ts
230  Text('This is the text content with letterSpacing 0.')
231    .letterSpacing(0)
232    .fontSize(12)
233    .border({ width: 1 })
234    .padding(10)
235    .width('100%')
236    .margin(5)
237  Text('This is the text content with letterSpacing 3.')
238    .letterSpacing(3)
239    .fontSize(12)
240    .border({ width: 1 })
241    .padding(10)
242    .width('100%')
243    .margin(5)
244  Text('This is the text content with letterSpacing -1.')
245    .letterSpacing(-1)
246    .fontSize(12)
247    .border({ width: 1 })
248    .padding(10)
249    .width('100%')
250    .margin(5)
251  ```
252
253  ![zh-cn_image_0000001562940513](figures/zh-cn_image_0000001562940513.png)
254
255- 通过[minFontSize](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#minfontsize)与[maxFontSize](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#maxfontsize)自适应字体大小。
256
257  minFontSize用于设置文本的最小显示字号,maxFontSize用于设置文本的最大显示字号。这两个属性必须同时设置才能生效,并且需要与[maxLines](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#maxlines)属性或布局大小限制配合使用,单独设置任一属性将不会产生效果。
258
259  ```ts
260  Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为1')
261    .width(250)
262    .maxLines(1)
263    .maxFontSize(30)
264    .minFontSize(5)
265    .border({ width: 1 })
266    .padding(10)
267    .margin(5)
268  Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为2')
269    .width(250)
270    .maxLines(2)
271    .maxFontSize(30)
272    .minFontSize(5)
273    .border({ width: 1 })
274    .padding(10)
275    .margin(5)
276  Text('我的最大字号为30,最小字号为15,宽度为250,高度为50')
277    .width(250)
278    .height(50)
279    .maxFontSize(30)
280    .minFontSize(15)
281    .border({ width: 1 })
282    .padding(10)
283    .margin(5)
284  Text('我的最大字号为30,最小字号为15,宽度为250,高度为100')
285    .width(250)
286    .height(100)
287    .maxFontSize(30)
288    .minFontSize(15)
289    .border({ width: 1 })
290    .padding(10)
291    .margin(5)
292  ```
293
294  ![zh-cn_image_0000001511740472](figures/zh-cn_image_0000001511740472.png)
295
296- 通过[textCase](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#textcase)属性设置文本大小写。
297
298  ```ts
299  Text('This is the text content with textCase set to Normal.')
300    .textCase(TextCase.Normal)
301    .padding(10)
302    .border({ width: 1 })
303    .padding(10)
304    .margin(5)
305  // 文本全小写展示
306  Text('This is the text content with textCase set to LowerCase.')
307    .textCase(TextCase.LowerCase)
308    .border({ width: 1 })
309    .padding(10)
310    .margin(5)
311  // 文本全大写展示
312  Text('This is the text content with textCase set to UpperCase.')
313    .textCase(TextCase.UpperCase)
314    .border({ width: 1 })
315    .padding(10)
316    .margin(5)
317  ```
318
319  ![zh-cn_image_0000001562940529](figures/zh-cn_image_0000001562940529.png)
320
321- 通过[copyOption](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#copyoption9)属性设置文本是否可复制粘贴。
322
323  ```ts
324  Text("这是一段可复制文本")
325    .fontSize(30)
326    .copyOption(CopyOptions.InApp)
327  ```
328
329  ![zh-cn_image_0000001511580868](figures/zh-cn_image_0000001511580868.png)
330
331
332## 添加事件
333
334Text组件可以添加通用事件,可以绑定[onClick](../reference/apis-arkui/arkui-ts/ts-universal-events-click.md#onclick)、[onTouch](../reference/apis-arkui/arkui-ts/ts-universal-events-touch.md#ontouch)等事件来响应操作。
335
336```ts
337Text('点我')
338  .onClick(() => {
339      console.info('我是Text的点击响应事件');
340   })
341```
342
343## 场景示例
344
345该示例通过maxLines、textOverflow、textAlign、constraintSize属性展示了热搜榜的效果。
346
347```ts
348// xxx.ets
349@Entry
350@Component
351struct TextExample {
352  build() {
353    Column() {
354      Row() {
355        Text("1").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
356        Text("我是热搜词条1")
357          .fontSize(12)
358          .fontColor(Color.Blue)
359          .maxLines(1)
360          .textOverflow({ overflow: TextOverflow.Ellipsis })
361          .fontWeight(300)
362        Text("爆")
363          .margin({ left: 6 })
364          .textAlign(TextAlign.Center)
365          .fontSize(10)
366          .fontColor(Color.White)
367          .fontWeight(600)
368          .backgroundColor(0x770100)
369          .borderRadius(5)
370          .width(15)
371          .height(14)
372      }.width('100%').margin(5)
373
374      Row() {
375        Text("2").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
376        Text("我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2")
377          .fontSize(12)
378          .fontColor(Color.Blue)
379          .fontWeight(300)
380          .constraintSize({ maxWidth: 200 })
381          .maxLines(1)
382          .textOverflow({ overflow: TextOverflow.Ellipsis })
383        Text("热")
384          .margin({ left: 6 })
385          .textAlign(TextAlign.Center)
386          .fontSize(10)
387          .fontColor(Color.White)
388          .fontWeight(600)
389          .backgroundColor(0xCC5500)
390          .borderRadius(5)
391          .width(15)
392          .height(14)
393      }.width('100%').margin(5)
394
395      Row() {
396        Text("3").fontSize(14).fontColor(Color.Orange).margin({ left: 10, right: 10 })
397        Text("我是热搜词条3")
398          .fontSize(12)
399          .fontColor(Color.Blue)
400          .fontWeight(300)
401          .maxLines(1)
402          .constraintSize({ maxWidth: 200 })
403          .textOverflow({ overflow: TextOverflow.Ellipsis })
404        Text("热")
405          .margin({ left: 6 })
406          .textAlign(TextAlign.Center)
407          .fontSize(10)
408          .fontColor(Color.White)
409          .fontWeight(600)
410          .backgroundColor(0xCC5500)
411          .borderRadius(5)
412          .width(15)
413          .height(14)
414      }.width('100%').margin(5)
415
416      Row() {
417        Text("4").fontSize(14).fontColor(Color.Grey).margin({ left: 10, right: 10 })
418        Text("我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4")
419          .fontSize(12)
420          .fontColor(Color.Blue)
421          .fontWeight(300)
422          .constraintSize({ maxWidth: 200 })
423          .maxLines(1)
424          .textOverflow({ overflow: TextOverflow.Ellipsis })
425      }.width('100%').margin(5)
426    }.width('100%')
427  }
428}
429
430```
431
432![zh-cn_image_0000001562820805](figures/zh-cn_image_0000001562820805.png)
433<!--RP1--><!--RP1End-->
434