1# Text 2 3The **\<Text>** component is used to display a piece of textual information. 4 5> **NOTE**<br/> 6> 7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11 12This component can contain the [\<Span>](ts-basic-components-span.md) child component. 13 14 15## APIs 16 17Text(content?: string | Resource) 18 19Since API version 9, this API is supported in ArkTS widgets. 20 21**Parameters** 22 23| Name| Type| Mandatory| Description| 24| -------- | -------- | -------- | -------- | 25| content | string \| [Resource](ts-types.md#resource) | No| Text content. The content and style set for the **\<Text>** component do not take effect when it contains the **\<Span>** child component.<br>Default value: **' '**| 26 27## Attributes 28 29In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. 30 31| Name | Type | Description | 32| ----------------------- | ----------------------------------- | ------------------------------------------- | 33| textAlign | [TextAlign](ts-appendix-enums.md#textalign) | Horizontal alignment mode of the text.<br>Default value: **TextAlign.Start**<br>**NOTE**<br>The text takes up the full width of the **\<Text>** component. To set the vertical alignment for the text, use the [align](ts-universal-attributes-location.md) attribute.<br>Since API version 9, this API is supported in ArkTS widgets.| 34| textOverflow | {overflow: [TextOverflow](ts-appendix-enums.md#textoverflow)} | Display mode when the text is too long.<br>Default value: **{overflow: TextOverflow.Clip}**<br>**NOTE**<br/>Text is clipped at the transition between words. To clip text in the middle of a word, add **\u200B** between characters.<br>This attribute must be used with `maxLines` to take effect.<br>Since API version 9, this API is supported in ArkTS widgets.| 35| maxLines | number | Maximum number of lines in the text.<br>Default value: **Infinity**<br>**NOTE**<br/>By default, text is automatically folded. If this attribute is specified, the text will not exceed the specified number of lines. If there is extra text, you can use **textOverflow** to specify how it is displayed.<br>Since API version 9, this API is supported in ArkTS widgets.| 36| lineHeight | string \| number \| [Resource](ts-types.md#resource) | Text line height. If the value is less than or equal to **0**, the line height is not limited and the font size is adaptive. If the value of the number type, the unit fp is used.<br>Since API version 9, this API is supported in ArkTS widgets.| 37| decoration | {<br>type: [TextDecorationType](ts-appendix-enums.md#textdecorationtype),<br>color?: [ResourceColor](ts-types.md#resourcecolor)<br>} | Style and color of the text decorative line.<br>Default value: {<br>type: TextDecorationType.None,<br>color: Color.Black<br>} <br>Since API version 9, this API is supported in ArkTS widgets.| 38| baselineOffset | number \| string | Baseline offset of the text. The default value is **0**.<br>Since API version 9, this API is supported in ArkTS widgets. | 39| letterSpacing | number \| string | Letter spacing.<br>Since API version 9, this API is supported in ArkTS widgets. | 40| minFontSize | number \| string \| [Resource](ts-types.md#resource) | Minimum font size.<br>For the setting to take effect, this attribute must be used together with **maxFontSize**, **maxline**, or a layout size constraint.<br>Since API version 9, this API is supported in ArkTS widgets. | 41| maxFontSize | number \| string \| [Resource](ts-types.md#resource) | Maximum font size.<br>For the setting to take effect, this attribute must be used together with **minFontSize**, **maxline**, or a layout size constraint.<br>Since API version 9, this API is supported in ArkTS widgets. | 42| textCase | [TextCase](ts-appendix-enums.md#textcase) | Text case.<br>Default value: **TextCase.Normal**<br>Since API version 9, this API is supported in ArkTS widgets.| 43| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.<br>Default value: **CopyOptions.None**<br>This API is supported in ArkTS widgets.| 44 45> **NOTE** 46> 47> The **\<Text>** component cannot contain both the text and the child component **\<Span>**. If both of them exist, only the content in **\<Span>** is displayed. 48 49 50## Example 51 52### Example 1 53Examples of using **textAlign**, **textOverflow**, **maxLines**, and **lineHeight** 54```ts 55// xxx.ets 56@Entry 57@Component 58struct TextExample1 { 59 build() { 60 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 61 // Set the horizontal alignment mode for the text. 62 // Single-line text 63 Text('textAlign').fontSize(9).fontColor(0xCCCCCC) 64 Text('TextAlign set to Center.') 65 .textAlign(TextAlign.Center) 66 .fontSize(12) 67 .border({ width: 1 }) 68 .padding(10) 69 .width('100%') 70 Text('TextAlign set to Start.') 71 .textAlign(TextAlign.Start) 72 .fontSize(12) 73 .border({ width: 1 }) 74 .padding(10) 75 .width('100%') 76 Text('TextAlign set to End.') 77 .textAlign(TextAlign.End) 78 .fontSize(12) 79 .border({ width: 1 }) 80 .padding(10) 81 .width('100%') 82 83 // Multi-line text 84 Text('This is the text content with textAlign set to Center.') 85 .textAlign(TextAlign.Center) 86 .fontSize(12) 87 .border({ width: 1 }) 88 .padding(10) 89 .width('100%') 90 Text('This is the text content with textAlign set to Start.') 91 .textAlign(TextAlign.Start) 92 .fontSize(12) 93 .border({ width: 1 }) 94 .padding(10) 95 .width('100%') 96 Text('This is the text content with textAlign set to End.') 97 .textAlign(TextAlign.End) 98 .fontSize(12) 99 .border({ width: 1 }) 100 .padding(10) 101 .width('100%') 102 103 104 // Set the display mode when the text is too long. 105 Text('TextOverflow+maxLines').fontSize(9).fontColor(0xCCCCCC) 106 // Clip the text when the value of maxLines is exceeded. 107 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.') 108 .textOverflow({ overflow: TextOverflow.None }) 109 .maxLines(1) 110 .fontSize(12) 111 .border({ width: 1 }) 112 .padding(10) 113 114 // Show an ellipsis (...) when the value of maxLines is exceeded. 115 Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('') 116 .join('\u200B')) 117 .textOverflow({ overflow: TextOverflow.Ellipsis }) 118 .maxLines(1) 119 .fontSize(12) 120 .border({ width: 1 }) 121 .padding(10) 122 123 Text('lineHeight').fontSize(9).fontColor(0xCCCCCC) 124 Text('This is the text with the line height set. This is the text with the line height set.') 125 .fontSize(12).border({ width: 1 }).padding(10) 126 Text('This is the text with the line height set. This is the text with the line height set.') 127 .fontSize(12).border({ width: 1 }).padding(10) 128 .lineHeight(20) 129 }.height(600).width(350).padding({ left: 35, right: 35, top: 35 }) 130 } 131} 132``` 133 134 135### Example 2 136Example of using **decoration**, **baselineOffset**, **letterSpacing**, and **textCase**: 137```ts 138@Entry 139@Component 140struct TextExample2 { 141 build() { 142 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 143 Text('decoration').fontSize(9).fontColor(0xCCCCCC) 144 Text('This is the text content with the decoration set to LineThrough and the color set to Red.') 145 .decoration({ 146 type: TextDecorationType.LineThrough, 147 color: Color.Red 148 }) 149 .fontSize(12) 150 .border({ width: 1 }) 151 .padding(10) 152 .width('100%') 153 154 155 Text('This is the text content with the decoration set to Overline and the color set to Red.') 156 .decoration({ 157 type: TextDecorationType.Overline, 158 color: Color.Red 159 }) 160 .fontSize(12) 161 .border({ width: 1 }) 162 .padding(10) 163 .width('100%') 164 165 166 Text('This is the text content with the decoration set to Underline and the color set to Red.') 167 .decoration({ 168 type: TextDecorationType.Underline, 169 color: Color.Red 170 }) 171 .fontSize(12) 172 .border({ width: 1 }) 173 .padding(10) 174 .width('100%') 175 176 // Set the text baseline offset. 177 Text('baselineOffset').fontSize(9).fontColor(0xCCCCCC) 178 Text('This is the text content with baselineOffset 0.') 179 .baselineOffset(0) 180 .fontSize(12) 181 .border({ width: 1 }) 182 .padding(10) 183 .width('100%') 184 Text('This is the text content with baselineOffset 30.') 185 .baselineOffset(30) 186 .fontSize(12) 187 .border({ width: 1 }) 188 .padding(10) 189 .width('100%') 190 Text('This is the text content with baselineOffset -20.') 191 .baselineOffset(-20) 192 .fontSize(12) 193 .border({ width: 1 }) 194 .padding(10) 195 .width('100%') 196 197 // Set the letter spacing. 198 Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC) 199 Text('This is the text content with letterSpacing 0.') 200 .letterSpacing(0) 201 .fontSize(12) 202 .border({ width: 1 }) 203 .padding(10) 204 .width('100%') 205 Text('This is the text content with letterSpacing 3.') 206 .letterSpacing(3) 207 .fontSize(12) 208 .border({ width: 1 }) 209 .padding(10) 210 .width('100%') 211 Text('This is the text content with letterSpacing -1.') 212 .letterSpacing(-1) 213 .fontSize(12) 214 .border({ width: 1 }) 215 .padding(10) 216 .width('100%') 217 218 Text('textCase').fontSize(9).fontColor(0xCCCCCC) 219 Text('This is the text content with textCase set to Normal.') 220 .textCase(TextCase.Normal) 221 .fontSize(12) 222 .border({ width: 1 }) 223 .padding(10) 224 .width('100%') 225 // Display the text in lowercase. 226 Text('This is the text content with textCase set to LowerCase.') 227 .textCase(TextCase.LowerCase) 228 .fontSize(12) 229 .border({ width: 1 }) 230 .padding(10) 231 .width('100%') 232 // Display the text in uppercase. 233 Text('This is the text content with textCase set to UpperCase.') 234 .textCase(TextCase.UpperCase) 235 .fontSize(12).border({ width: 1 }).padding(10) 236 237 }.height(700).width(350).padding({ left: 35, right: 35, top: 35 }) 238 } 239} 240``` 241 242