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

E
ester.zhou 已提交
3
The **\<Text>** component is used to display a piece of textual information.
Z
zengyawen 已提交
4

5 6 7 8
>  **NOTE**
>
>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

Z
zengyawen 已提交
9 10

## Required Permissions
Z
zengyawen 已提交
11

12
None
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16

## Child Components

E
ester.zhou 已提交
17
This component can contain the [\<Span>](ts-basic-components-span.md) child component.
Z
zengyawen 已提交
18 19 20 21 22 23 24


## APIs

Text(content?: string)

- Parameters
E
ester.zhou 已提交
25
  | Name| Type| Mandatory| Default Value| Description|
Z
zengyawen 已提交
26
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
27
  | content | string | No| '' | Text content. This parameter does not take effect when the child component **\<Span>** is contained.|
Z
zengyawen 已提交
28 29 30 31


## Attributes

E
ester.zhou 已提交
32
| Name| Type| Default Value| Description|
Z
zengyawen 已提交
33
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
34
| textAlign | TextAlign | TextAlign.Start | Text alignment mode of multiple lines of text.|
35 36
| textOverflow | {overflow: TextOverflow} | {overflow: TextOverflow.Clip} | Display mode when the text is too long.<br>**NOTE**<br/><br>Text is truncated at the transition between words. To truncate text in the middle of a word, add **\u200B** between characters.<br>This attribute must be used with `maxLines` to take effect.|
| maxLines | number | Infinity | Maximum number of lines in the text.<br>**NOTE**<br/><br>By default, text is automatically folded. If this parameter is specified, the text does not exceed the specified number of lines. If there is extra text, you can use `textOverflow` to specify the truncation mode.|
E
ester.zhou 已提交
37 38 39 40 41
| lineHeight | Length | - | 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.|
| decoration | {<br>type: TextDecorationType,<br>color?: Color<br>} | {<br>type: TextDecorationType.None,<br>color: Color.Black<br>} | Style and color of the text decorative line.|
| baselineOffset | Length | - | Offset of the text baseline.|
| textCase | TextCase | TextCase.Normal | Text case.|
| copyOption<sup>9+</sup> | boolean\|CopyOption | false | Whether copy and paste is allowed.|
Z
zengyawen 已提交
42 43

- TextAlign enums
E
ester.zhou 已提交
44
  | Name| Description|
Z
zengyawen 已提交
45
  | -------- | -------- |
E
ester.zhou 已提交
46 47 48
  | Center | The text is center-aligned.|
  | Start | The text is aligned with the direction in which the text is written.|
  | End | The text is aligned with the opposite direction in which the text is written.|
Z
zengyawen 已提交
49 50

- TextOverflow enums
E
ester.zhou 已提交
51
  | Name| Description|
Z
zengyawen 已提交
52
  | -------- | -------- |
E
ester.zhou 已提交
53 54 55
  | Clip | Extra text is truncated.|
  | Ellipsis | An ellipsis (...) is used to represent clipped text.|
  | None | No truncation or ellipsis is used for extra-long text.|
Z
zengyawen 已提交
56 57

- TextDecorationType enums
E
ester.zhou 已提交
58
  | Name| Description|
Z
zengyawen 已提交
59
  | -------- | -------- |
E
ester.zhou 已提交
60 61 62 63
  | Underline | Line under the text.|
  | LineThrough | Line through the text.|
  | Overline | Line over the text.|
  | None | No decorative lines.|
Z
zengyawen 已提交
64 65

- TextCase enums
E
ester.zhou 已提交
66 67 68 69 70 71 72 73 74 75
  
  | Name     | Description                |
  | --------- | -------------------- |
  | Normal    | The original case of the text is retained.|
  | LowerCase | All letters in the text are in lowercase.    |
  | UpperCase | All letters in the text are in uppercase.    |
  
- CopyOption<sup>9+</sup> enums
  
  | Name| Description|
Z
zengyawen 已提交
76
  | -------- | -------- |
E
ester.zhou 已提交
77 78 79
  | InApp | Intra-application copy and paste is allowed.|
  | LocalDevice | Intra-device copy and paste is allowed.|
  | CrossDevice | Cross-device copy and paste is allowed.|
Z
zengyawen 已提交
80

81
> **NOTE**<br/>
E
ester.zhou 已提交
82
> If the **\<Text>** component contains both the text and the **\<Span>** child component, only the content in **\<Span>** is displayed.
Z
zengyawen 已提交
83 84 85 86


## Example

E
ester.zhou 已提交
87 88
```ts
// xxx.ets
Z
zengyawen 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
@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)
E
ester.zhou 已提交
105
      Text('This is set textOverflow to Ellipsis text content This is set textOverflow to Ellipsis text content.'.split('').join('\u200B'))
Z
zengyawen 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        .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 已提交
124 125
![en-us_image_0000001257138337](figures/en-us_image_0000001257138337.gif)

E
ester.zhou 已提交
126 127
```ts
// xxx.ets
Z
zengyawen 已提交
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 166
@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 已提交
167
![en-us_image_0000001257058391](figures/en-us_image_0000001257058391.gif)