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

Z
zengyawen 已提交
3
文本,用于呈现一段信息。
Z
zengyawen 已提交
4

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

Z
zengyawen 已提交
9

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

Z
zengyawen 已提交
12

Z
zengyawen 已提交
13 14


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

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


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

L
luoying_ace_admin 已提交
22
Text(content?: ResourceStr)
Z
zengyawen 已提交
23

H
HelloCrease 已提交
24
**参数:**
Z
zengyawen 已提交
25

H
HelloCrease 已提交
26 27
| 参数名     | 参数类型   | 必填   | 默认值  | 参数描述                                     |
| ------- | ------ | ---- | ---- | ---------------------------------------- |
L
luoying_ace_admin 已提交
28
| content | [ResourceStr](../../ui/ts-types.md) | 否    | ''   | 文本内容。包含子组件Span时不生效,显示Span内容,并且此时text组件的样式不生效。 |
Z
zengyawen 已提交
29

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

H
HelloCrease 已提交
32 33
| 名称             | 参数类型                                     | 默认值                                      | 描述                                       |
| -------------- | ---------------------------------------- | ---------------------------------------- | ---------------------------------------- |
K
kangchongtao 已提交
34 35
| textAlign      | [TextAlign](ts-appendix-enums.md#textalign)       | TextAlign.Start                          | 设置多行文本的文本对齐方式。                           |
| textOverflow   | {overflow:&nbsp;[TextOverflow](ts-appendix-enums.md#textoverflow)}            | {overflow:&nbsp;TextOverflow.Clip}       | 设置文本超长时的显示方式。<br/>**说明:**<br/>文本截断是按字截断。例如,英文以单词为最小单位进行截断,若需要以字母为单位进行截断,可在字母间添加零宽空格:\u200B。 |
H
HelloCrease 已提交
36
| maxLines       | number                                   | Infinity                                 | 设置文本的最大行数。                               |
L
luoying_ace_admin 已提交
37
| lineHeight     | string&nbsp;\|&nbsp;number&nbsp;\|&nbsp;[Resource](../../ui/ts-types.md)  | - | 设置文本的文本行高,设置值不大于0时,不限制文本行高,自适应字体大小,Length为number类型时单位为fp。 |
K
kangchongtao 已提交
38
| decoration     | {<br/>type:&nbsp;[TextDecorationType](ts-appendix-enums.md#textdecorationtype),<br/>color?:&nbsp;[ResourceColor](../../ui/ts-types.md)<br/>} | {<br/>type:&nbsp;TextDecorationType.None,<br/>color:Color.Black<br/>} | 设置文本装饰线样式及其颜色。                           |
L
luoying_ace_admin 已提交
39
| baselineOffset | [Length](../../ui/ts-types.md)         | -         | 设置文本基线的偏移量。                              |
K
kangchongtao 已提交
40
| textCase       | [TextCase](ts-appendix-enums.md#textcase)          | TextCase.Normal                          | 设置文本大小写。                                 |
H
HelloCrease 已提交
41 42 43 44 45


>  **说明:**
>
>  不支持Text内同时存在文本内容和Span子组件。(如果同时存在,只显示Span内的内容)。
Z
zengyawen 已提交
46 47


Z
zengyawen 已提交
48
## 示例
Z
zengyawen 已提交
49

H
HelloCrease 已提交
50 51
```ts
// xxx.ets
Z
zengyawen 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
@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)
68
      Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('').join('\u200B'))
Z
zengyawen 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        .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 已提交
87
![zh-cn_image_0000001219864155](figures/zh-cn_image_0000001219864155.gif)
Z
zengyawen 已提交
88

H
HelloCrease 已提交
89 90
```ts
// xxx.ets
Z
zengyawen 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
@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 已提交
130
![zh-cn_image_0000001174422918](figures/zh-cn_image_0000001174422918.gif)