1# Text Display (Text/Span) 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 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/arkui-ts/ts-basic-components-span.md) component can only act as a child of the [\<Text>](../reference/arkui-ts/ts-basic-components-text.md) and [\<RichEditor>](../reference/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 }).padding(10) 141 Text('I am extra long text, with an ellipse displayed for any excess.') 142 .width(250) 143 .textOverflow({ overflow: TextOverflow.Ellipsis }) 144 .maxLines(1) 145 .fontSize(12) 146 .border({ width: 1 }).padding(10) 147 Text ('When the text overflows the container, it scrolls.') 148 .width(250) 149 .textOverflow({ overflow: TextOverflow.MARQUEE }) 150 .maxLines(1) 151 .fontSize(12) 152 .border({ width: 1 }).padding(10) 153 ``` 154 155  156 157- Use the **lineHeight** attribute to set the text line height. 158 159 ```ts 160 Text('This is the text with the line height set. This is the text with the line height set.') 161 .width(300).fontSize(12).border({ width: 1 }).padding(10) 162 Text('This is the text with the line height set. This is the text with the line height set.') 163 .width(300).fontSize(12).border({ width: 1 }).padding(10) 164 .lineHeight(20) 165 ``` 166 167  168 169- Use the **decoration** attribute to set the style and color of the text decorative line. 170 171 ```ts 172 Text('This is the text') 173 .decoration({ 174 type: TextDecorationType.LineThrough, 175 color: Color.Red 176 }) 177 .borderWidth(1).padding(10).margin(5) 178 Text('This is the text') 179 .decoration({ 180 type: TextDecorationType.Overline, 181 color: Color.Red 182 }) 183 .borderWidth(1).padding(10).margin(5) 184 Text('This is the text') 185 .decoration({ 186 type: TextDecorationType.Underline, 187 color: Color.Red 188 }) 189 .borderWidth(1).padding(10).margin(5) 190 ``` 191 192  193 194- Use the **baselineOffset** attribute to set the baseline offset of the text. 195 196 ```ts 197 Text('This is the text content with baselineOffset 0.') 198 .baselineOffset(0) 199 .fontSize(12) 200 .border({ width: 1 }) 201 .padding(10) 202 .width('100%') 203 .margin(5) 204 Text('This is the text content with baselineOffset 30.') 205 .baselineOffset(30) 206 .fontSize(12) 207 .border({ width: 1 }) 208 .padding(10) 209 .width('100%') 210 .margin(5) 211 212 Text('This is the text content with baselineOffset -20.') 213 .baselineOffset(-20) 214 .fontSize(12) 215 .border({ width: 1 }) 216 .padding(10) 217 .width('100%') 218 .margin(5) 219 ``` 220 221  222 223- Use the **letterSpacing** attribute to set the letter spacing. 224 225 ```ts 226 Text('This is the text content with letterSpacing 0.') 227 .letterSpacing(0) 228 .fontSize(12) 229 .border({ width: 1 }) 230 .padding(10) 231 .width('100%') 232 .margin(5) 233 Text('This is the text content with letterSpacing 3.') 234 .letterSpacing(3) 235 .fontSize(12) 236 .border({ width: 1 }) 237 .padding(10) 238 .width('100%') 239 .margin(5) 240 Text('This is the text content with letterSpacing -1.') 241 .letterSpacing(-1) 242 .fontSize(12) 243 .border({ width: 1 }) 244 .padding(10) 245 .width('100%') 246 .margin(5) 247 ``` 248 249  250 251- 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. 252 253 ```ts 254 Text('My maximum font size is 30, minimum font size is 5, width is 250, and maximum number of lines is 1') 255 .width(250) 256 .maxLines(1) 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 5, width is 250, and maximum number of lines is 2') 263 .width(250) 264 .maxLines(2) 265 .maxFontSize(30) 266 .minFontSize(5) 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 50') 271 .width(250) 272 .height(50) 273 .maxFontSize(30) 274 .minFontSize(15) 275 .border({ width: 1 }) 276 .padding(10) 277 .margin(5) 278 Text('My maximum font size is 30, minimum font size is 15, width is 250, and line height is 100') 279 .width(250) 280 .height(100) 281 .maxFontSize(30) 282 .minFontSize(15) 283 .border({ width: 1 }) 284 .padding(10) 285 .margin(5) 286 ``` 287 288  289 290- Use the **textCase** attribute to set the text case. 291 292 ```ts 293 Text('This is the text content with textCase set to Normal.') 294 .textCase(TextCase.Normal) 295 .padding(10) 296 .border({ width: 1 }) 297 .padding(10) 298 .margin(5) 299 300 // The text is displayed in lowercase. 301 Text('This is the text content with textCase set to LowerCase.') 302 .textCase(TextCase.LowerCase) 303 .border({ width: 1 }) 304 .padding(10) 305 .margin(5) 306 307 // The text is displayed in uppercase. 308 Text('This is the text content with textCase set to UpperCase.') 309 .textCase(TextCase.UpperCase) 310 .border({ width: 1 }) 311 .padding(10) 312 .margin(5) 313 ``` 314 315  316 317- Use the **copyOption** attribute to set whether copy and paste is allowed. 318 319 ```ts 320 Text("This text is copyable") 321 .fontSize(30) 322 .copyOption(CopyOptions.InApp) 323 ``` 324 325  326 327 328## Adding Events 329 330The **\<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. 331 332 333```ts 334Text ('Click Me') 335 .onClick(()=>{ 336 console.info('I am the response to the click event'); 337 }) 338``` 339 340 341## Example Scenario 342 343 344```ts 345// xxx.ets 346@Entry 347@Component 348struct TextExample { 349 build() { 350 Column() { 351 Row() { 352 Text("1").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 }) 353 Text("I am entry 1") 354 .fontSize(12) 355 .fontColor(Color.Blue) 356 .maxLines(1) 357 .textOverflow({ overflow: TextOverflow.Ellipsis }) 358 .fontWeight(300) 359 Text ("Top Hit") 360 .margin({ left: 6 }) 361 .textAlign(TextAlign.Center) 362 .fontSize(10) 363 .fontColor(Color.White) 364 .fontWeight(600) 365 .backgroundColor(0x770100) 366 .borderRadius(5) 367 .width(15) 368 .height(14) 369 }.width('100%').margin(5) 370 371 Row() { 372 Text("2").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 }) 373 Text("I am entry 2") 374 .fontSize(12) 375 .fontColor(Color.Blue) 376 .fontWeight(300) 377 .constraintSize({ maxWidth: 200 }) 378 .maxLines(1) 379 .textOverflow({ overflow: TextOverflow.Ellipsis }) 380 Text ("Hot") 381 .margin({ left: 6 }) 382 .textAlign(TextAlign.Center) 383 .fontSize(10) 384 .fontColor(Color.White) 385 .fontWeight(600) 386 .backgroundColor(0xCC5500) 387 .borderRadius(5) 388 .width(15) 389 .height(14) 390 }.width('100%').margin(5) 391 392 Row() { 393 Text("3").fontSize(14).fontColor(Color.Orange).margin({ left: 10, right: 10 }) 394 Text("I am entry 3") 395 .fontSize(12) 396 .fontColor(Color.Blue) 397 .fontWeight(300) 398 .maxLines(1) 399 .constraintSize({ maxWidth: 200 }) 400 .textOverflow({ overflow: TextOverflow.Ellipsis }) 401 Text ("Hot") 402 .margin({ left: 6 }) 403 .textAlign(TextAlign.Center) 404 .fontSize(10) 405 .fontColor(Color.White) 406 .fontWeight(600) 407 .backgroundColor(0xCC5500) 408 .borderRadius(5) 409 .width(15) 410 .height(14) 411 }.width('100%').margin(5) 412 413 Row() { 414 Text("4").fontSize(14).fontColor(Color.Grey).margin({ left: 10, right: 10 }) 415 Text("I am entry 4") 416 .fontSize(12) 417 .fontColor(Color.Blue) 418 .fontWeight(300) 419 .constraintSize({ maxWidth: 200 }) 420 .maxLines(1) 421 .textOverflow({ overflow: TextOverflow.Ellipsis }) 422 }.width('100%').margin(5) 423 }.width('100%') 424 } 425} 426 427``` 428 429 430