1# Text Display (Text/Span) 2 3 4The **\<Text>** component is used to display a piece of textual information. For details, see [Text](../reference/apis-arkui/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 20 21 22- Referencing Resource objects 23 24 You can use **$r** to create a **Resource** object to reference resources in **/resources/base/element/string.json**. 25 26 27 ```ts 28 Text($r('app.string.module_desc')) 29 .baselineOffset(0) 30 .fontSize(30) 31 .border({ width: 1 }) 32 .padding(10) 33 .width(300) 34 ``` 35 36  37 38 39## Adding Child Components 40 41The [\<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. 42 43- Creating a \<Span> Component 44 45 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. 46 47 48 ```ts 49 Text (' I'm Text') { 50 Span (' I'm Span') 51 } 52 .padding(10) 53 .borderWidth(1) 54 ``` 55 56  57 58- Set the text decorative line. 59 60 Use the **decoration** attribute to set the style and color of the text decorative line. 61 62 63 ```ts 64 Text() { 65 Span('I'm Span1,') .fontSize (16).fontColor (Color.Grey) 66 .decoration({ type: TextDecorationType.LineThrough, color: Color.Red }) 67 Span('I'm Span2').fontColor (Color.Blue).fontSize (16) 68 .fontStyle(FontStyle.Italic) 69 .decoration({ type: TextDecorationType.Underline, color: Color.Black }) 70 Span('I'm Span3').fontSize(16).fontColor(Color.Grey) 71 .decoration({ type: TextDecorationType.Overline, color: Color.Green }) 72 } 73 .borderWidth(1) 74 .padding(10) 75 ``` 76 77  78 79- Use the **textCase** attribute to set the text case. 80 81 ```ts 82 Text() { 83 Span('I am Upper-span').fontSize(12) 84 .textCase(TextCase.UpperCase) 85 } 86 .borderWidth(1) 87 .padding(10) 88 ``` 89 90  91 92- Adding Events 93 94 The **\<Span>** component does not have size information. Therefore, only an **onClick** event can be added for this component. 95 96 97 ```ts 98 Text() { 99 Span('I am Upper-span').fontSize(12) 100 .textCase(TextCase.UpperCase) 101 .onClick(()=>{ 102 console.info ('I'm Span - onClick') 103 }) 104 } 105 ``` 106 107 108## Setting Styles 109 110- Use the **textAlign** attribute to set the alignment mode of text. 111 112 ```ts 113 Text('Left aligned') 114 .width(300) 115 .textAlign(TextAlign.Start) 116 .border({ width: 1 }) 117 .padding(10) 118 Text ('Center aligned') 119 .width(300) 120 .textAlign(TextAlign.Center) 121 .border({ width: 1 }) 122 .padding(10) 123 Text('Right aligned') 124 .width(300) 125 .textAlign(TextAlign.End) 126 .border({ width: 1 }) 127 .padding(10) 128 ``` 129 130  131 132- 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. 133 134 ```ts 135 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.') 136 .width(250) 137 .textOverflow({ overflow: TextOverflow.None }) 138 .maxLines(1) 139 .fontSize(12) 140 .border({ width: 1 }) 141 .padding(10) 142 Text('I am extra long text, with an ellipse displayed for any excess.') 143 .width(250) 144 .textOverflow({ overflow: TextOverflow.Ellipsis }) 145 .maxLines(1) 146 .fontSize(12) 147 .border({ width: 1 }) 148 .padding(10) 149 Text ('When the text overflows the container, it scrolls.') 150 .width(250) 151 .textOverflow({ overflow: TextOverflow.MARQUEE }) 152 .maxLines(1) 153 .fontSize(12) 154 .border({ width: 1 }) 155 .padding(10) 156 ``` 157 158  159 160- Use the **lineHeight** attribute to set the text line height. 161 162 ```ts 163 Text('This is the text with the line height set. This is the text with the line height set.') 164 .width(300).fontSize(12).border({ width: 1 }).padding(10) 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 .lineHeight(20) 168 ``` 169 170  171 172- Use the **decoration** attribute to set the style and color of the text decorative line. 173 174 ```ts 175 Text('This is the text') 176 .decoration({ 177 type: TextDecorationType.LineThrough, 178 color: Color.Red 179 }) 180 .borderWidth(1).padding(10).margin(5) 181 Text('This is the text') 182 .decoration({ 183 type: TextDecorationType.Overline, 184 color: Color.Red 185 }) 186 .borderWidth(1).padding(10).margin(5) 187 Text('This is the text') 188 .decoration({ 189 type: TextDecorationType.Underline, 190 color: Color.Red 191 }) 192 .borderWidth(1).padding(10).margin(5) 193 ``` 194 195  196 197- Use the **baselineOffset** attribute to set the baseline offset of the text. 198 199 ```ts 200 Text('This is the text content with baselineOffset 0.') 201 .baselineOffset(0) 202 .fontSize(12) 203 .border({ width: 1 }) 204 .padding(10) 205 .width('100%') 206 .margin(5) 207 Text('This is the text content with baselineOffset 30.') 208 .baselineOffset(30) 209 .fontSize(12) 210 .border({ width: 1 }) 211 .padding(10) 212 .width('100%') 213 .margin(5) 214 215 Text('This is the text content with baselineOffset -20.') 216 .baselineOffset(-20) 217 .fontSize(12) 218 .border({ width: 1 }) 219 .padding(10) 220 .width('100%') 221 .margin(5) 222 ``` 223 224  225 226- Use the **letterSpacing** attribute to set the letter spacing. 227 228 ```ts 229 Text('This is the text content with letterSpacing 0.') 230 .letterSpacing(0) 231 .fontSize(12) 232 .border({ width: 1 }) 233 .padding(10) 234 .width('100%') 235 .margin(5) 236 Text('This is the text content with letterSpacing 3.') 237 .letterSpacing(3) 238 .fontSize(12) 239 .border({ width: 1 }) 240 .padding(10) 241 .width('100%') 242 .margin(5) 243 Text('This is the text content with letterSpacing -1.') 244 .letterSpacing(-1) 245 .fontSize(12) 246 .border({ width: 1 }) 247 .padding(10) 248 .width('100%') 249 .margin(5) 250 ``` 251 252  253 254- 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. 255 256 ```ts 257 Text('My maximum font size is 30, minimum font size is 5, width is 250, and maximum number of lines is 1') 258 .width(250) 259 .maxLines(1) 260 .maxFontSize(30) 261 .minFontSize(5) 262 .border({ width: 1 }) 263 .padding(10) 264 .margin(5) 265 Text('My maximum font size is 30, minimum font size is 5, width is 250, and maximum number of lines is 2') 266 .width(250) 267 .maxLines(2) 268 .maxFontSize(30) 269 .minFontSize(5) 270 .border({ width: 1 }) 271 .padding(10) 272 .margin(5) 273 Text('My maximum font size is 30, minimum font size is 15, width is 250, and line height is 50') 274 .width(250) 275 .height(50) 276 .maxFontSize(30) 277 .minFontSize(15) 278 .border({ width: 1 }) 279 .padding(10) 280 .margin(5) 281 Text('My maximum font size is 30, minimum font size is 15, width is 250, and line height is 100') 282 .width(250) 283 .height(100) 284 .maxFontSize(30) 285 .minFontSize(15) 286 .border({ width: 1 }) 287 .padding(10) 288 .margin(5) 289 ``` 290 291  292 293- Use the **textCase** attribute to set the text case. 294 295 ```ts 296 Text('This is the text content with textCase set to Normal.') 297 .textCase(TextCase.Normal) 298 .padding(10) 299 .border({ width: 1 }) 300 .padding(10) 301 .margin(5) 302 303 // The text is displayed in lowercase. 304 Text('This is the text content with textCase set to LowerCase.') 305 .textCase(TextCase.LowerCase) 306 .border({ width: 1 }) 307 .padding(10) 308 .margin(5) 309 310 // The text is displayed in uppercase. 311 Text('This is the text content with textCase set to UpperCase.') 312 .textCase(TextCase.UpperCase) 313 .border({ width: 1 }) 314 .padding(10) 315 .margin(5) 316 ``` 317 318  319 320- Use the **copyOption** attribute to set whether copy and paste is allowed. 321 322 ```ts 323 Text("This text is copyable") 324 .fontSize(30) 325 .copyOption(CopyOptions.InApp) 326 ``` 327 328  329 330 331## Adding Events 332 333The **\<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. 334 335 336```ts 337Text ('Click Me') 338 .onClick(()=>{ 339 console.info('I am the response to the click event'); 340 }) 341``` 342 343 344## Example Scenario 345 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("I am entry 1") 357 .fontSize(12) 358 .fontColor(Color.Blue) 359 .maxLines(1) 360 .textOverflow({ overflow: TextOverflow.Ellipsis }) 361 .fontWeight(300) 362 Text ("Top Hit") 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("I am entry 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 ("Hot") 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("I am entry 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 ("Hot") 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("I am entry 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 433