1# Text 2 3显示一段文本的组件。 4 5> **说明:** 6> 7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12可以包含[Span](ts-basic-components-span.md)和[ImageSpan](ts-basic-components-imagespan.md)子组件。 13 14 15## 接口 16 17Text(content?: string | Resource) 18 19从API version 9开始,该接口支持在ArkTS卡片中使用。 20 21**参数:** 22 23| 参数名 | 参数类型 | 必填 | 参数描述 | 24| -------- | -------- | -------- | -------- | 25| content | string \| [Resource](ts-types.md#resource) | 否 | 文本内容。包含子组件Span时不生效,显示Span内容,并且此时text组件的样式不生效。<br/>默认值:' ' | 26 27## 属性 28 29除支持[通用属性](ts-universal-attributes-size.md)和[文本通用属性](ts-universal-attributes-text-style.md)外,还支持以下属性: 30 31| 名称 | 参数类型 | 描述 | 32| ----------------------- | ----------------------------------- | ------------------------------------------- | 33| textAlign | [TextAlign](ts-appendix-enums.md#textalign) | 设置文本段落在水平方向的对齐方式。<br/>默认值:TextAlign.Start<br/>说明:<br/>文本段落宽度占满Text组件宽度;可通过[align](ts-universal-attributes-location.md)属性控制文本段落在垂直方向上的位置,此组件中不可通过align属性控制文本段落在水平方向上的位置,即align属性中Alignment.TopStart、Alignment.Top、Alignment.TopEnd效果相同,控制内容在顶部,Alignment.Start、Alignment.Center、Alignment.End效果相同,控制内容垂直居中,Alignment.BottomStart、Alignment.Bottom、Alignment.BottomEnd效果相同,控制内容在底部。结合TextAlign属性可控制内容在水平方向的位置。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 | 34| textOverflow | {overflow: [TextOverflow](ts-appendix-enums.md#textoverflow)} | 设置文本超长时的显示方式。<br/>默认值:{overflow: TextOverflow.Clip}<br/>**说明:**<br/>文本截断是按字截断。例如,英文以单词为最小单位进行截断,若需要以字母为单位进行截断,可在字母间添加零宽空格:\u200B。<br />当`overflow`设置为TextOverflow.None、TextOverflow.Clip、TextOverflow.Ellipsis时,需配合`maxLines`使用,单独设置不生效。设置TextOverflow.None与TextOverflow.Clip效果一样。当`overflow`设置为TextOverflow.Marquee时,文本在一行内滚动显示,设置`maxLines`及`copyOption`属性均不生效。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 | 35| maxLines | number | 设置文本的最大行数。<br />默认值:Infinity<br/>**说明:**<br />默认情况下,文本是自动折行的,如果指定此参数,则文本最多不会超过指定的行。如果有多余的文本,可以通过 `textOverflow`来指定截断方式。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 | 36| lineHeight | string \| number \| [Resource](ts-types.md#resource) | 设置文本的文本行高,设置值不大于0时,不限制文本行高,自适应字体大小,Length为number类型时单位为fp。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 | 37| decoration | {<br/>type: [TextDecorationType](ts-appendix-enums.md#textdecorationtype),<br/>color?: [ResourceColor](ts-types.md#resourcecolor)<br/>} | 设置文本装饰线样式及其颜色。<br />默认值:{<br/>type: TextDecorationType.None,<br/>color:Color.Black<br/>} <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。| 38| baselineOffset | number \| string | 设置文本基线的偏移量,默认值0。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>设置该值为百分比时,按默认值显示。 | 39| letterSpacing | number \| string | 设置文本字符间距。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>设置该值为百分比时,按默认值显示。<br /> 当取值为负值时,文字会发生重叠。 | 40| minFontSize | number \| string \| [Resource](ts-types.md#resource) | 设置文本最小显示字号。<br/>需配合maxFontSize以及maxline或布局大小限制使用,单独设置不生效。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 | 41| maxFontSize | number \| string \| [Resource](ts-types.md#resource) | 设置文本最大显示字号。<br/>需配合minFontSize以及maxline或布局大小限制使用,单独设置不生效。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 | 42| textCase | [TextCase](ts-appendix-enums.md#textcase) | 设置文本大小写。<br />默认值:TextCase.Normal <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。| 43| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | 组件支持设置文本是否可复制粘贴。<br />默认值:CopyOptions.None <br/>该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>设置copyOptions为CopyOptions.InApp或者CopyOptions.LocalDevice,长按文本,会弹出文本选择菜单,可选中文本并进行复制、全选操作。 | 44| draggable | boolean | 设置选中文本拖拽效果。<br/>不能和[onDragStart](ts-universal-events-drag-drop.md)事件同时使用;<br/>需配合copyOption一起使用,设置copyOptions为CopyOptions.InApp或者CopyOptions.LocalDevice,并且dragable设置为true时,支持对选中文本的拖拽以及选中内容复制到输入框。<br/>默认值:false <br />**说明:**<br />从 API version 9 开始支持。 | 45| textShadow<sup>10+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明) | 设置文字阴影效果。 | 46| heightAdaptivePolicy<sup>10+</sup> | [TextHeightAdaptivePolicy](ts-appendix-enums.md#textheightadaptivepolicy10) | 设置文本自适应高度的方式。<br/>默认值:TextHeightAdaptivePolicy.MAX_LINES_FIRST。<br/>**说明:**<br/>当设置为TextHeightAdaptivePolicy.MAX_LINES_FIRST时,优先使用`maxLines`属性来调整文本高度。如果使用`maxLines`属性的布局大小超过了布局约束,则尝试在`minFontSize`和`maxFontSize`的范围内缩小字体以显示更多文本。<br/>当设置为TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST时,优先使用`minFontSize`属性来调整文本高度。如果使用`minFontSize`属性可以将文本布局在一行中,则尝试在`minFontSize`和`maxFontSize`的范围内增大字体并使用最大可能的字体大小。<br/>当设置为TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST时,优先使用布局约束来调整文本高度。如果布局大小超过布局约束,则尝试在`minFontSize`和`maxFontSize`的范围内缩小字体以满足布局约束。如果将字体大小缩小到`minFontSize`后,布局大小仍然超过布局约束,则删除超过布局约束的行。| 47| textIndent<sup>10+</sup> | number \| string | 设置首行文本缩进,默认值0。 | 48| font<sup>10+</sup> | [Font](ts-types.md#font) | 设置文本样式。包括字体大小、字体粗细、字体族和字体风格。 | 49 50> **说明:** 51> 52> 不支持Text内同时存在文本内容和Span或ImageSpan子组件。如果同时存在,只显示Span或ImageSpan内的内容。 53> 54> 通用属性中形状裁剪[clip](ts-universal-attributes-sharp-clipping.md#clip)属性,在Text默认值为true。 55 56## 事件 57 58支持[通用事件](ts-universal-events-click.md)。 59 60 61## 示例 62 63### 示例1 64 65```ts 66// xxx.ets 67@Entry 68@Component 69struct TextExample1 { 70 build() { 71 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 72 // 文本水平方向对齐方式设置 73 // 单行文本 74 Text('textAlign').fontSize(9).fontColor(0xCCCCCC) 75 Text('TextAlign set to Center.') 76 .textAlign(TextAlign.Center) 77 .fontSize(12) 78 .border({ width: 1 }) 79 .padding(10) 80 .width('100%') 81 Text('TextAlign set to Start.') 82 .textAlign(TextAlign.Start) 83 .fontSize(12) 84 .border({ width: 1 }) 85 .padding(10) 86 .width('100%') 87 Text('TextAlign set to End.') 88 .textAlign(TextAlign.End) 89 .fontSize(12) 90 .border({ width: 1 }) 91 .padding(10) 92 .width('100%') 93 94 // 多行文本 95 Text('This is the text content with textAlign set to Center.') 96 .textAlign(TextAlign.Center) 97 .fontSize(12) 98 .border({ width: 1 }) 99 .padding(10) 100 .width('100%') 101 Text('This is the text content with textAlign set to Start.') 102 .textAlign(TextAlign.Start) 103 .fontSize(12) 104 .border({ width: 1 }) 105 .padding(10) 106 .width('100%') 107 Text('This is the text content with textAlign set to End.') 108 .textAlign(TextAlign.End) 109 .fontSize(12) 110 .border({ width: 1 }) 111 .padding(10) 112 .width('100%') 113 114 115 // 文本超长时显示方式 116 Text('TextOverflow+maxLines').fontSize(9).fontColor(0xCCCCCC) 117 // 超出maxLines截断内容展示 118 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.') 119 .textOverflow({ overflow: TextOverflow.Clip }) 120 .maxLines(1) 121 .fontSize(12) 122 .border({ width: 1 }) 123 .padding(10) 124 125 // 超出maxLines展示省略号 126 Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('') 127 .join('\u200B')) 128 .textOverflow({ overflow: TextOverflow.Ellipsis }) 129 .maxLines(1) 130 .fontSize(12) 131 .border({ width: 1 }) 132 .padding(10) 133 134 Text('lineHeight').fontSize(9).fontColor(0xCCCCCC) 135 Text('This is the text with the line height set. This is the text with the line height set.') 136 .fontSize(12).border({ width: 1 }).padding(10) 137 Text('This is the text with the line height set. This is the text with the line height set.') 138 .fontSize(12).border({ width: 1 }).padding(10) 139 .lineHeight(20) 140 }.height(600).width(350).padding({ left: 35, right: 35, top: 35 }) 141 } 142} 143``` 144 145 146### 示例2 147 148```ts 149@Entry 150@Component 151struct TextExample2 { 152 build() { 153 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 154 Text('decoration').fontSize(9).fontColor(0xCCCCCC) 155 Text('This is the text content with the decoration set to LineThrough and the color set to Red.') 156 .decoration({ 157 type: TextDecorationType.LineThrough, 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 Overline and the color set to Red.') 167 .decoration({ 168 type: TextDecorationType.Overline, 169 color: Color.Red 170 }) 171 .fontSize(12) 172 .border({ width: 1 }) 173 .padding(10) 174 .width('100%') 175 176 177 Text('This is the text content with the decoration set to Underline and the color set to Red.') 178 .decoration({ 179 type: TextDecorationType.Underline, 180 color: Color.Red 181 }) 182 .fontSize(12) 183 .border({ width: 1 }) 184 .padding(10) 185 .width('100%') 186 187 // 文本基线偏移 188 Text('baselineOffset').fontSize(9).fontColor(0xCCCCCC) 189 Text('This is the text content with baselineOffset 0.') 190 .baselineOffset(0) 191 .fontSize(12) 192 .border({ width: 1 }) 193 .padding(10) 194 .width('100%') 195 Text('This is the text content with baselineOffset 30.') 196 .baselineOffset(30) 197 .fontSize(12) 198 .border({ width: 1 }) 199 .padding(10) 200 .width('100%') 201 Text('This is the text content with baselineOffset -20.') 202 .baselineOffset(-20) 203 .fontSize(12) 204 .border({ width: 1 }) 205 .padding(10) 206 .width('100%') 207 208 // 文本字符间距 209 Text('letterSpacing').fontSize(9).fontColor(0xCCCCCC) 210 Text('This is the text content with letterSpacing 0.') 211 .letterSpacing(0) 212 .fontSize(12) 213 .border({ width: 1 }) 214 .padding(10) 215 .width('100%') 216 Text('This is the text content with letterSpacing 3.') 217 .letterSpacing(3) 218 .fontSize(12) 219 .border({ width: 1 }) 220 .padding(10) 221 .width('100%') 222 Text('This is the text content with letterSpacing -1.') 223 .letterSpacing(-1) 224 .fontSize(12) 225 .border({ width: 1 }) 226 .padding(10) 227 .width('100%') 228 229 Text('textCase').fontSize(9).fontColor(0xCCCCCC) 230 Text('This is the text content with textCase set to Normal.') 231 .textCase(TextCase.Normal) 232 .fontSize(12) 233 .border({ width: 1 }) 234 .padding(10) 235 .width('100%') 236 // 文本全小写展示 237 Text('This is the text content with textCase set to LowerCase.') 238 .textCase(TextCase.LowerCase) 239 .fontSize(12) 240 .border({ width: 1 }) 241 .padding(10) 242 .width('100%') 243 // 文本全大写展示 244 Text('This is the text content with textCase set to UpperCase.') 245 .textCase(TextCase.UpperCase) 246 .fontSize(12).border({ width: 1 }).padding(10) 247 248 }.height(700).width(350).padding({ left: 35, right: 35, top: 35 }) 249 } 250} 251``` 252 253 254### 示例3 255 256```ts 257@Entry 258@Component 259struct TextExample { 260 build() { 261 Column({ space: 8 }) { 262 // 设置文字阴影效果 263 Text('textShadow').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 264 Text('textShadow') 265 .width('80%') 266 .height(55) 267 .fontSize(40) 268 .lineHeight(55) 269 .textAlign(TextAlign.Center) 270 .textShadow({ radius: 10, color: Color.Black, offsetX: 0, offsetY: 0 }) 271 .borderWidth(1) 272 Divider() 273 // 设置文本自适应高度的方式 274 Text('heightAdaptivePolicy').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 275 Text('This is the text with the height adaptive policy set') 276 .width('80%') 277 .height(90) 278 .borderWidth(1) 279 .minFontSize(10) 280 .maxFontSize(30) 281 .maxLines(3) 282 .textOverflow({ overflow: TextOverflow.Ellipsis }) 283 .heightAdaptivePolicy(TextHeightAdaptivePolicy.MAX_LINES_FIRST) 284 Text('This is the text with the height adaptive policy set') 285 .width('80%') 286 .height(90) 287 .borderWidth(1) 288 .minFontSize(10) 289 .maxFontSize(30) 290 .maxLines(3) 291 .textOverflow({ overflow: TextOverflow.Ellipsis }) 292 .heightAdaptivePolicy(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST) 293 Text('This is the text with the height adaptive policy set') 294 .width('80%') 295 .height(90) 296 .borderWidth(1) 297 .minFontSize(10) 298 .maxFontSize(30) 299 .maxLines(3) 300 .textOverflow({ overflow: TextOverflow.Ellipsis }) 301 .heightAdaptivePolicy(TextHeightAdaptivePolicy.LAYOUT_CONSTRAINT_FIRST) 302 Divider() 303 // 设置文本超长时以跑马灯的方式展示 304 Text('marquee').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 305 Text('This is the text with the text overflow set marquee') 306 .width(300) 307 .borderWidth(1) 308 .textOverflow({ overflow: TextOverflow.MARQUEE }) 309 } 310 } 311} 312``` 313 314