ts-basic-components-text.md 7.4 KB
Newer Older
Z
zengyawen 已提交
1
# Text
Z
zengyawen 已提交
2

T
explain  
tianyu 已提交
3 4
显示一段文本的组件。

H
geshi  
HelloCrease 已提交
5
>  **说明:**
Z
zengyawen 已提交
6
> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
7 8


Z
zengyawen 已提交
9
## 权限列表
Z
zengyawen 已提交
10

Z
zengyawen 已提交
11

Z
zengyawen 已提交
12 13


Z
zengyawen 已提交
14
## 子组件
Z
zengyawen 已提交
15

Z
zengyawen 已提交
16
可以包含[Span](ts-basic-components-span.md)子组件。
Z
zengyawen 已提交
17 18


Z
zengyawen 已提交
19
## 接口
Z
zengyawen 已提交
20

Z
zengyawen 已提交
21
Text(content?: string)
Z
zengyawen 已提交
22

Z
zengyawen 已提交
23
- 参数
T
explain  
tianyu 已提交
24
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
Z
zengyawen 已提交
25
  | -------- | -------- | -------- | -------- | -------- |
T
explain  
tianyu 已提交
26
  | content | string | 否 | '' | 文本内容,包含子组件Span时不生效,显示Span内容。 |
Z
zengyawen 已提交
27 28


Z
zengyawen 已提交
29
## 属性
Z
zengyawen 已提交
30

Z
zengyawen 已提交
31 32 33
| 名称 | 参数类型 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- |
| textAlign | TextAlign | TextAlign.Start | 设置多行文本的文本对齐方式。 |
34 35
| textOverflow | {overflow:&nbsp;TextOverflow} | {overflow:&nbsp;TextOverflow.Clip} | 设置文本超长时的显示方式。<br/>**说明:**<br/>文本截断是按字截断。例如,英文以单词为最小单位进行截断,若需要以字母为单位进行截断,可在字母间添加零宽空格:\u200B。<br />需配合`maxLines`使用,单独设置不生效。 |
| maxLines | number | Infinity | 设置文本的最大行数。<br />**说明:**<br />默认情况下,文本是自动折行的,如果指定此参数,则文本最多不会超过指定的行。如果有多余的文本,可以通过        `textOverflow`来指定截断方式。 |
Z
zengyawen 已提交
36
| lineHeight | Length | - | 设置文本的文本行高,设置值不大于0时,不限制文本行高,自适应字体大小,Length为number类型时单位为fp。 |
37
| decoration | {<br/>type:&nbsp;TextDecorationType,<br/>color?:&nbsp;Color<br/>} | {<br/>type:&nbsp;TextDecorationType.None,<br/>color: Color.Black<br/>} | 设置文本装饰线样式及其颜色。 |
Z
zengyawen 已提交
38 39
| baselineOffset | Length | - | 设置文本基线的偏移量。 |
| textCase | TextCase | TextCase.Normal | 设置文本大小写。 |
W
wzztoone 已提交
40
| copyOption<sup>9+</sup> | boolean\|CopyOption | false | 组件支持设置文本是否可复制粘贴。 |
Z
zengyawen 已提交
41

Z
zengyawen 已提交
42
- TextAlign枚举说明
T
explain  
tianyu 已提交
43
  | 名称 | 描述 |
Z
zengyawen 已提交
44
  | -------- | -------- |
T
explain  
tianyu 已提交
45 46 47
  | Center | 文本居中对齐。 |
  | Start | 根据文字书写相同的方向对齐。 |
  | End | 根据文字书写相反的方向对齐。 |
Z
zengyawen 已提交
48

Z
zengyawen 已提交
49
- TextOverflow枚举说明
T
explain  
tianyu 已提交
50
  | 名称 | 描述 |
Z
zengyawen 已提交
51
  | -------- | -------- |
T
explain  
tianyu 已提交
52 53 54
  | Clip | 文本超长时进行裁剪显示。 |
  | Ellipsis | 文本超长时显示不下的文本用省略号代替。 |
  | None | 文本超长时不进行裁剪。 |
Z
zengyawen 已提交
55

Z
zengyawen 已提交
56
- TextDecorationType枚举说明
T
explain  
tianyu 已提交
57
  | 名称 | 描述 |
Z
zengyawen 已提交
58
  | -------- | -------- |
T
explain  
tianyu 已提交
59 60 61 62
  | Underline | 文字下划线修饰。 |
  | LineThrough | 穿过文本的修饰线。 |
  | Overline | 文字上划线修饰。 |
  | None | 不使用文本装饰线。 |
Z
zengyawen 已提交
63

Z
zengyawen 已提交
64
- TextCase枚举说明
W
wzztoone 已提交
65 66 67 68 69 70 71 72 73 74
  
  | 名称      | 描述                 |
  | --------- | -------------------- |
  | Normal    | 保持文本原有大小写。 |
  | LowerCase | 文本采用全小写。     |
  | UpperCase | 文本采用全大写。     |
  
- CopyOption<sup>9+</sup>枚举说明
  
  | 名称 | 描述 |
Z
zengyawen 已提交
75
  | -------- | -------- |
W
wzztoone 已提交
76 77 78
  | InApp | 支持应用内复制粘贴。 |
  | LocalDevice | 支持设备内复制粘贴。 |
  | CrossDevice | 支持跨设备复制粘贴。 |
Z
zengyawen 已提交
79

H
geshi  
HelloCrease 已提交
80
>  **说明:**
81
> 不支持Text内同时存在文本内容和Span子组件。如果同时存在,只显示Span内的内容。
Z
zengyawen 已提交
82 83


Z
zengyawen 已提交
84
## 示例
Z
zengyawen 已提交
85

H
geshi  
HelloCrease 已提交
86 87
```ts
// xxx.ets
Z
zengyawen 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
@Entry
@Component
struct TextExample1 {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Text('lineHeight').fontSize(9).fontColor(0xCCCCCC)
      Text('This is the text with the line height set This is the text with the line height set This is the text with the line height set.')
        .lineHeight(25).fontSize(12).border({ width: 1 }).padding(10)

      Text('TextOverflow').fontSize(9).fontColor(0xCCCCCC)
      Text('This is the setting of textOverflow to none text content This is the setting of textOverflow to none text content.')
        .textOverflow({ overflow: TextOverflow.None })
        .fontSize(12).border({ width: 1 }).padding(10)
      Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to Clip text content.')
        .textOverflow({ overflow: TextOverflow.Clip })
        .maxLines(1).fontSize(12).border({ width: 1 }).padding(10)
C
caocan 已提交
104
      Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('').join('\u200B'))
Z
zengyawen 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .maxLines(1).fontSize(12).border({ width: 1 }).padding(10)

      Text('decoration').fontSize(9).fontColor(0xCCCCCC)
      Text('This is the text content with the decoration set to Underline and the color set to Red.')
        .decoration({ type: TextDecorationType.Underline, color: Color.Red })
        .fontSize(12).border({ width: 1 }).padding(10)
      Text('This is the text content with the decoration set to LineThrough and the color set to Red.')
        .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
        .fontSize(12).border({ width: 1 }).padding(10)
      Text('This is the text content with the decoration set to Overline and the color set to Red.')
        .decoration({ type: TextDecorationType.Overline, color: Color.Red })
        .fontSize(12).border({ width: 1 }).padding(10)
    }.height(600).width(350).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Z
zengyawen 已提交
123
![zh-cn_image_0000001219864155](figures/zh-cn_image_0000001219864155.gif)
Z
zengyawen 已提交
124

H
geshi  
HelloCrease 已提交
125 126
```ts
// xxx.ets
Z
zengyawen 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
@Entry
@Component
struct TextExample2 {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Text('textCase').fontSize(9).fontColor(0xCCCCCC)
      Text('This is the text content with textCase set to Normal.')
        .textCase(TextCase.Normal)
        .fontSize(12).border({ width: 1 }).padding(10).width('100%')
      Text('This is the text content with textCase set to LowerCase.')
        .textCase(TextCase.LowerCase)
        .fontSize(12).border({ width: 1 }).padding(10).width('100%')
      Text('This is the text content with textCase set to UpperCase.')
        .textCase(TextCase.UpperCase)
        .fontSize(12).border({ width: 1 }).padding(10)

      Text('textAlign').fontSize(9).fontColor(0xCCCCCC)
      Text('This is the text content with textAlign set to Center.')
        .textAlign(TextAlign.Center)
        .fontSize(12).border({ width: 1 }).padding(10).width('100%')
      Text('This is the text content with textAlign set to Start.')
        .textAlign(TextAlign.Start)
        .fontSize(12).border({ width: 1 }).padding(10).width('100%')
      Text('This is the text content with textAlign set to End.')
        .textAlign(TextAlign.End)
        .fontSize(12).border({ width: 1 }).padding(10).width('100%')

      Text('baselineOffset').fontSize(9).fontColor(0xCCCCCC)
      Text('This is the text content with baselineOffset set to 10.')
        .baselineOffset(10).fontSize(12).border({ width: 1 }).padding(10).width('100%')
      Text('This is the text content with baselineOffset set to 30.')
        .baselineOffset(30).fontSize(12).border({ width: 1 }).padding(10).width('100%')
      Text('This is the text content with baselineOffset set to -10.')
        .baselineOffset(-10).fontSize(12).border({ width: 1 }).padding(10).width('100%')
    }.height(700).width(350).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Z
zengyawen 已提交
166
![zh-cn_image_0000001174422918](figures/zh-cn_image_0000001174422918.gif)