1# Text Display (Text/Span) 2 3 4The **Text** component is used to display a piece of textual information. The **Span** component is used to display inline text. 5 6For details, see [Text](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md) and [Span](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md). 7 8 9## Creating Text 10 11You can create text in either of the following ways: 12 13 14- Entering strings 15 16 ```ts 17 Text('I am a piece of text') 18 ``` 19 20 21 22 23 24- Referencing Resource objects 25 26 You can use **$r** to create a **Resource** object to reference resources in **/resources/base/element/string.json**. 27 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  39 40 41## Adding Child Components 42 43The [Span](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md) component can only act as a child of the [Text](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md) and [RichEditor](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md) components. 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. 44 45- Creating a Span Component 46 47 The **Span** component works only when included in a **Text** component. It does not display any content when used alone. If both the **Span** and **Text** components have text configured, the text of the **Span** overwrites that of the **Text** component. 48 49 50 ```ts 51 Text('I am Text') { 52 Span('I am Span') 53 } 54 .padding(10) 55 .borderWidth(1) 56 ``` 57 58  59 60- Set the text decorative line. 61 62 Use the [decoration](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md#decoration) attribute to set the style and color of the text decorative line. 63 64 65 ```ts 66 Text() { 67 Span('I am Span1,').fontSize(16).fontColor(Color.Grey) 68 .decoration({ type: TextDecorationType.LineThrough, color: Color.Red }) 69 Span('I am Span2').fontColor(Color.Blue).fontSize(16) 70 .fontStyle(FontStyle.Italic) 71 .decoration({ type: TextDecorationType.Underline, color: Color.Black }) 72 Span('I am Span3').fontSize(16).fontColor(Color.Grey) 73 .decoration({ type: TextDecorationType.Overline, color: Color.Green }) 74 } 75 .borderWidth(1) 76 .padding(10) 77 ``` 78 79  80 81- Use the [textCase](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md#textcase) attribute to set the text case. 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  93 94- Add events. 95 96 The **Span** component does not have size information. Therefore, only an [onClick](../reference/apis-arkui/arkui-ts/ts-universal-events-click.md#onclick) event can be added for this component. 97 98 99 ```ts 100 Text() { 101 Span('I am Upper-span').fontSize(12) 102 .textCase(TextCase.UpperCase) 103 .onClick(()=>{ 104 console.info('I am Span - onClick') 105 }) 106 } 107 ``` 108 109 110## Setting Styles 111 112- Use the [textAlign](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#textalign) attribute to set the alignment mode of text. 113 114 ```ts 115 Text('Left aligned') 116 .width(300) 117 .textAlign(TextAlign.Start) 118 .border({ width: 1 }) 119 .padding(10) 120 Text('Center aligned') 121 .width(300) 122 .textAlign(TextAlign.Center) 123 .border({ width: 1 }) 124 .padding(10) 125 Text('Right aligned') 126 .width(300) 127 .textAlign(TextAlign.End) 128 .border({ width: 1 }) 129 .padding(10) 130 ``` 131 132  133 134- Use the [textOverflow](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#textoverflow) attribute to set the display mode for when the text is too long. This attribute must be used together with [maxLines](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#maxlines). By default, the text is automatically wrapped. 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 extra long text, with an ellipse 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 the container, it scrolls.') 152 .width(250) 153 .textOverflow({ overflow: TextOverflow.MARQUEE }) 154 .maxLines(1) 155 .fontSize(12) 156 .border({ width: 1 }) 157 .padding(10) 158 ``` 159 160  161 162- Use the [lineHeight](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#lineheight) attribute to set the text line height. 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  173 174- Use the [decoration](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#decoration) attribute to set the style and color of the text decorative line. 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  198 199- Use the [baselineOffset](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#baselineoffset) attribute to set the baseline offset of the text. 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 217 Text('This is the text content with baselineOffset -20.') 218 .baselineOffset(-20) 219 .fontSize(12) 220 .border({ width: 1 }) 221 .padding(10) 222 .width('100%') 223 .margin(5) 224 ``` 225 226  227 228- Use the [letterSpacing](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#letterspacing) attribute to set the letter spacing. 229 230 ```ts 231 Text('This is the text content with letterSpacing 0.') 232 .letterSpacing(0) 233 .fontSize(12) 234 .border({ width: 1 }) 235 .padding(10) 236 .width('100%') 237 .margin(5) 238 Text('This is the text content with letterSpacing 3.') 239 .letterSpacing(3) 240 .fontSize(12) 241 .border({ width: 1 }) 242 .padding(10) 243 .width('100%') 244 .margin(5) 245 Text('This is the text content with letterSpacing -1.') 246 .letterSpacing(-1) 247 .fontSize(12) 248 .border({ width: 1 }) 249 .padding(10) 250 .width('100%') 251 .margin(5) 252 ``` 253 254  255 256- Use the [minFontSize](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#minfontsize) and [maxFontSize](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#maxfontsize) attributes to set the minimum and maximum font size, respectively. 257 258 For the settings to take effect, these attributes must be used together with [maxLines](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#maxlines) or layout constraint settings. 259 260 ```ts 261 Text('My maximum font size is 30, minimum font size is 5, width is 250, and maximum number of lines is 1') 262 .width(250) 263 .maxLines(1) 264 .maxFontSize(30) 265 .minFontSize(5) 266 .border({ width: 1 }) 267 .padding(10) 268 .margin(5) 269 Text('My maximum font size is 30, minimum font size is 5, width is 250, and maximum number of lines is 2') 270 .width(250) 271 .maxLines(2) 272 .maxFontSize(30) 273 .minFontSize(5) 274 .border({ width: 1 }) 275 .padding(10) 276 .margin(5) 277 Text('My maximum font size is 30, minimum font size is 15, width is 250, and line height is 50') 278 .width(250) 279 .height(50) 280 .maxFontSize(30) 281 .minFontSize(15) 282 .border({ width: 1 }) 283 .padding(10) 284 .margin(5) 285 Text('My maximum font size is 30, minimum font size is 15, width is 250, and line height is 100') 286 .width(250) 287 .height(100) 288 .maxFontSize(30) 289 .minFontSize(15) 290 .border({ width: 1 }) 291 .padding(10) 292 .margin(5) 293 ``` 294 295  296 297- Use the [textCase](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#textcase) attribute to set the text case. 298 299 ```ts 300 Text('This is the text content with textCase set to Normal.') 301 .textCase(TextCase.Normal) 302 .padding(10) 303 .border({ width: 1 }) 304 .padding(10) 305 .margin(5) 306 307 // The text is displayed in lowercase. 308 Text('This is the text content with textCase set to LowerCase.') 309 .textCase(TextCase.LowerCase) 310 .border({ width: 1 }) 311 .padding(10) 312 .margin(5) 313 314 // The text is displayed in uppercase. 315 Text('This is the text content with textCase set to UpperCase.') 316 .textCase(TextCase.UpperCase) 317 .border({ width: 1 }) 318 .padding(10) 319 .margin(5) 320 ``` 321 322  323 324- Use the [copyOption](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#copyoption9) attribute to set whether copy and paste is allowed. 325 326 ```ts 327 Text("This text is copyable") 328 .fontSize(30) 329 .copyOption(CopyOptions.InApp) 330 ``` 331 332  333 334 335## Adding Events 336 337You can bind the **Text** component to the [onClick](../reference/apis-arkui/arkui-ts/ts-universal-events-click.md#onclick), [onTouch](../reference/apis-arkui/arkui-ts/ts-universal-events-touch.md#ontouch), or other universal events to respond to user operations. 338 339```ts 340Text('Click Me') 341 .onClick(()=>{ 342 console.info('I am the response to the click event'); 343 }) 344``` 345 346## Example Scenario 347 348This example demonstrates how to implement a hot search list using the **maxLines**, **textOverflow**, **textAlign**, and **constraintSize** attributes. 349 350```ts 351// xxx.ets 352@Entry 353@Component 354struct TextExample { 355 build() { 356 Column() { 357 Row() { 358 Text("1").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 }) 359 Text("I am entry 1") 360 .fontSize(12) 361 .fontColor(Color.Blue) 362 .maxLines(1) 363 .textOverflow({ overflow: TextOverflow.Ellipsis }) 364 .fontWeight(300) 365 Text("Top Hit") 366 .margin({ left: 6 }) 367 .textAlign(TextAlign.Center) 368 .fontSize(10) 369 .fontColor(Color.White) 370 .fontWeight(600) 371 .backgroundColor(0x770100) 372 .borderRadius(5) 373 .width(15) 374 .height(14) 375 }.width('100%').margin(5) 376 377 Row() { 378 Text("2").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 }) 379 Text("I am entry 2 I am entry 2 I am entry 2 I am entry 2") 380 .fontSize(12) 381 .fontColor(Color.Blue) 382 .fontWeight(300) 383 .constraintSize({ maxWidth: 200 }) 384 .maxLines(1) 385 .textOverflow({ overflow: TextOverflow.Ellipsis }) 386 Text("Hot") 387 .margin({ left: 6 }) 388 .textAlign(TextAlign.Center) 389 .fontSize(10) 390 .fontColor(Color.White) 391 .fontWeight(600) 392 .backgroundColor(0xCC5500) 393 .borderRadius(5) 394 .width(15) 395 .height(14) 396 }.width('100%').margin(5) 397 398 Row() { 399 Text("3").fontSize(14).fontColor(Color.Orange).margin({ left: 10, right: 10 }) 400 Text("I am entry 3") 401 .fontSize(12) 402 .fontColor(Color.Blue) 403 .fontWeight(300) 404 .maxLines(1) 405 .constraintSize({ maxWidth: 200 }) 406 .textOverflow({ overflow: TextOverflow.Ellipsis }) 407 Text("Hot") 408 .margin({ left: 6 }) 409 .textAlign(TextAlign.Center) 410 .fontSize(10) 411 .fontColor(Color.White) 412 .fontWeight(600) 413 .backgroundColor(0xCC5500) 414 .borderRadius(5) 415 .width(15) 416 .height(14) 417 }.width('100%').margin(5) 418 419 Row() { 420 Text("4").fontSize(14).fontColor(Color.Grey).margin({ left: 10, right: 10 }) 421 Text("I am entry 4 I am entry 4 I am entry 4 I am entry 4") 422 .fontSize(12) 423 .fontColor(Color.Blue) 424 .fontWeight(300) 425 .constraintSize({ maxWidth: 200 }) 426 .maxLines(1) 427 .textOverflow({ overflow: TextOverflow.Ellipsis }) 428 }.width('100%').margin(5) 429 }.width('100%') 430 } 431} 432 433``` 434 435 436<!--RP1--><!--RP1End--> 437