1e41f4b71Sopenharmony_ci# text
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>  **说明:**
4e41f4b71Sopenharmony_ci>
5e41f4b71Sopenharmony_ci>  - 该组件从API version 4开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>  - 文本的展示内容需要写在元素标签内。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci文本,用于呈现一段信息。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 权限列表
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## 子组件
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci支持<[span](js-components-basic-span.md)>。
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci## 属性
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci支持[通用属性](js-components-common-attributes.md)。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci## 样式
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci除支持[通用样式](js-components-common-styles.md)外,还支持如下样式:
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci| 名称                                 | 类型                                       | 默认值                                      | 必填   | 描述                                       |
31e41f4b71Sopenharmony_ci| ---------------------------------- | ---------------------------------------- | ---------------------------------------- | ---- | ---------------------------------------- |
32e41f4b71Sopenharmony_ci| color                              | <color>                            | #e5000000                                | 否    | 设置文本的颜色。                                 |
33e41f4b71Sopenharmony_ci| font-size                          | <length>                           | 30px                                     | 否    | 设置文本的尺寸。                                 |
34e41f4b71Sopenharmony_ci| allow-scale                        | boolean                                  | true                                     | 否    | 文本尺寸是否跟随系统设置字体缩放尺寸进行放大缩小。<br/>如果需要支持动态生效,请参看config描述文件中config-changes标签。 |
35e41f4b71Sopenharmony_ci| letter-spacing                     | &lt;length&gt;                           | 0px                                      | 否    | 设置文本的字符间距。                               |
36e41f4b71Sopenharmony_ci| word-spacing<sup>7+</sup>          | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;&nbsp;\|&nbsp;string | normal                                   | 否    | 设置文本之间的间距,string可选值为:<br/>normal:默认的字间距。 |
37e41f4b71Sopenharmony_ci| font-style                         | string                                   | normal                                   | 否    | 设置文本的字体样式,可选值为:<br/>-&nbsp;normal:标准的字体样式;<br/>-&nbsp;italic:斜体的字体样式。 |
38e41f4b71Sopenharmony_ci| font-weight                        | number&nbsp;\|&nbsp;string               | normal                                   | 否    | 设置文本的字体粗细,number类型取值[100,&nbsp;900],默认为400,取值越大,字体越粗。number取值必须为100的整数倍。<br/>string类型取值支持如下四个值:lighter、normal、bold、bolder。 |
39e41f4b71Sopenharmony_ci| text-decoration                    | string                                   | none                                     | 否    | 设置文本的文本修饰,可选值为:<br/>-&nbsp;underline:文字下划线修饰;<br/>-&nbsp;line-through:穿过文本的修饰线n<br/>-&nbsp;none:标准文本。 |
40e41f4b71Sopenharmony_ci| text-decoration-color<sup>7+</sup> | &lt;color&gt;                            | -                                        | 否    | 设置文本修饰线的颜色。                              |
41e41f4b71Sopenharmony_ci| text-align                         | string                                   | start<br/>                               | 否    | 设置文本的文本对齐方式,可选值为:<br/>-&nbsp;left:文本左对齐;<br/>-&nbsp;center:文本居中对齐;<br/>-&nbsp;right:文本右对齐;<br/>-&nbsp;start:根据文字书写相同的方向对齐;<br/>-&nbsp;end:根据文字书写相反的方向对齐。<br/>如果文本宽度未指定大小,文本的宽度和父容器的宽度大小相等的情况下,对齐效果可能会不明显。 |
42e41f4b71Sopenharmony_ci| line-height                        | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>7+</sup>&nbsp;\|&nbsp;string<sup>7+</sup> | 0px<sup>1-6</sup><br/>normal<sup>7+</sup> | 否    | 设置文本的文本行高,设置为0px时,不限制文本行高,自适应字体大小。string可选值为:<br/>normal<sup>7+</sup>:默认的行高。 |
43e41f4b71Sopenharmony_ci| text-overflow                      | string                                   | clip                                     | 否    | 在设置了最大行数的情况下生效,可选值为:<br/>-&nbsp;clip:将文本根据父容器大小进行裁剪显示;<br/>-&nbsp;ellipsis:根据父容器大小显示,显示不下的文本用省略号代替。需配合max-lines使用。 |
44e41f4b71Sopenharmony_ci| font-family                        | string                                   | sans-serif                               | 否    | 设置文本的字体列表,用逗号分隔,每个字体用字体名或者字体族名设置。列表中第一个系统中存在的或者通过[自定义字体](js-components-common-customizing-font.md)指定的字体,会被选中作为文本的字体。 |
45e41f4b71Sopenharmony_ci| max-lines                          | number&nbsp;\|&nbsp;string<sup>7+</sup>  | -                                        | 否    | 设置文本的最大行数,string类型可选值为:<br/>-&nbsp;auto<sup>7+</sup>:文本行数自适应容器高度。 |
46e41f4b71Sopenharmony_ci| min-font-size                      | &lt;length&gt;                           | -                                        | 否    | 文本最小字号,需要和文本最大字号同时设置,支持文本字号动态变化。设置最大最小字体样式后,font-size不生效。 |
47e41f4b71Sopenharmony_ci| max-font-size                      | &lt;length&gt;                           | -                                        | 否    | 文本最大字号,需要和文本最小字号同时设置,支持文本字号动态变化。设置最大最小字体样式后,font-size不生效。 |
48e41f4b71Sopenharmony_ci| font-size-step                     | &lt;length&gt;                           | 1px                                      | 否    | 文本动态调整字号时的步长,需要设置最小,最大字号样式生效。            |
49e41f4b71Sopenharmony_ci| prefer-font-sizes                  | &lt;array&gt;                            | -                                        | 否    | 预设的字号集合,在动态尺寸调整时,优先使用预设字号集合中的字号匹配设置的最大行数,如果预设字号集合未设置,则使用最大最小和步长调整字号。针对仍然无法满足最大行数要求的情况,使用text-overflow设置项进行截断,设置预设尺寸集后,font-size、max-font-size、min-font-size和font-size-step不生效。<br/>如:prefer-font-sizes:&nbsp;12px,14px,16px |
50e41f4b71Sopenharmony_ci| word-break<sup>6+</sup>            | string                                   | normal                                   | 否    | 设置文本折行模式,可选值为:<br/>-&nbsp;normal:默认换行规则,依据各自语言的规则,允许在字间发生换行。<br/>-&nbsp;break-all:对于非中文/日文/韩文的文本,可在任意字符间断行。<br/>-&nbsp;break-word:与break-all相同,不同的地方在于它要求一个没有断行破发点的词必须保持为一个整体单位。 |
51e41f4b71Sopenharmony_ci| text-indent<sup>7+</sup>           | &lt;length&gt;                           | -                                        | 否    | 设置首行缩进量。                                 |
52e41f4b71Sopenharmony_ci| white-space<sup>7+</sup>           | string                                   | pre                                      | 否    | 设置处理元素中空白的模式,可选值为:<br/>-&nbsp;normal:所有空格、回车、制表符都合并成一个空格,文本自动换行;<br/>-&nbsp;nowrap:所有空格、回车、制表符都合并成一个空格,文本不换行;<br/>-&nbsp;pre:所有东西原样输出;<br/>-&nbsp;pre-wrap:所有东西原样输出,文本换行;<br/>-&nbsp;pre-line:所有空格、制表符合并成一个空格,回车不变,文本换行。 |
53e41f4b71Sopenharmony_ci| adapt-height<sup>7+</sup>          | boolean                                  | false                                    | 否    | 文本大小是否自适应容器高度。<br/>设置字体大小自适应相关样式后生效。     |
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci>  **说明:**
56e41f4b71Sopenharmony_ci>  - 字体动态缩放:预设尺寸集合和最小最大字号调节基于是否满足最大行数要求,预设尺寸集合会按照从左到右顺序查看是否满足最大行数要求,最小最大字号调节则基于从大到小顺序查看是否满足最大行数要求。
57e41f4b71Sopenharmony_ci>
58e41f4b71Sopenharmony_ci>  - 文本换行:文本可以通过转义字符\r\n进行换行。
59e41f4b71Sopenharmony_ci>
60e41f4b71Sopenharmony_ci>  - 文本标签内支持以下转义字符:\a,\b,\f,\n,\r,\t,\v,\',\",\0。
61e41f4b71Sopenharmony_ci>
62e41f4b71Sopenharmony_ci>  - 当使用子组件span组成文本段落时,如果span属性样式异常,将导致text段落无法显示。
63e41f4b71Sopenharmony_ci>
64e41f4b71Sopenharmony_ci>  - letter-spacing、text-align、line-height、text-overflow和max-lines样式作用于text及其子组件(span)组成的文本内容。
65e41f4b71Sopenharmony_ci>
66e41f4b71Sopenharmony_ci>  - text组件说明:不支持text内同时存在文本内容和span子组件。(如果同时存在,只显示span内的内容)
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci## 事件
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci支持[通用事件](js-components-common-events.md)。
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci## 方法
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci支持[通用方法](js-components-common-methods.md)。
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci## 示例
79e41f4b71Sopenharmony_ci1. ​
80e41f4b71Sopenharmony_ci```html
81e41f4b71Sopenharmony_ci<!-- xxx.hml -->
82e41f4b71Sopenharmony_ci<div class="container">
83e41f4b71Sopenharmony_ci    <text class="title">default text</text>
84e41f4b71Sopenharmony_ci    <text class="title textcolor">hello world with color</text>
85e41f4b71Sopenharmony_ci    <text class="title textsize">hello world with font-size</text>
86e41f4b71Sopenharmony_ci    <text class="title textletterspacing">hello world with letter-spacing</text>
87e41f4b71Sopenharmony_ci    <text class="title textwordspacing">hello world with word-spacing</text>
88e41f4b71Sopenharmony_ci    <text class="title textstyle">hello world with italic</text>
89e41f4b71Sopenharmony_ci    <text class="title textweight">hello world with font-weight</text>
90e41f4b71Sopenharmony_ci    <text class="title textdecoration1">hello world with underline</text>
91e41f4b71Sopenharmony_ci    <text class="title textdecoration2">hello world with line-through</text>
92e41f4b71Sopenharmony_ci    <text class="title textalign">hello world with text-align:right</text>
93e41f4b71Sopenharmony_ci</div>
94e41f4b71Sopenharmony_ci```
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci```css
97e41f4b71Sopenharmony_ci/* xxx.css */
98e41f4b71Sopenharmony_ci.container {
99e41f4b71Sopenharmony_ci    display: flex;
100e41f4b71Sopenharmony_ci    justify-content: center;
101e41f4b71Sopenharmony_ci    align-items: center;
102e41f4b71Sopenharmony_ci    flex-direction: column;
103e41f4b71Sopenharmony_ci}
104e41f4b71Sopenharmony_ci.title {
105e41f4b71Sopenharmony_ci    text-align: center;
106e41f4b71Sopenharmony_ci    width: 800px;
107e41f4b71Sopenharmony_ci    height: 60px;
108e41f4b71Sopenharmony_ci}
109e41f4b71Sopenharmony_ci.textcolor {
110e41f4b71Sopenharmony_ci    color: indianred;
111e41f4b71Sopenharmony_ci}
112e41f4b71Sopenharmony_ci.textsize {
113e41f4b71Sopenharmony_ci    font-size: 40px;
114e41f4b71Sopenharmony_ci}
115e41f4b71Sopenharmony_ci.textletterspacing {
116e41f4b71Sopenharmony_ci    letter-spacing: -3px;
117e41f4b71Sopenharmony_ci}
118e41f4b71Sopenharmony_ci.textwordspacing {
119e41f4b71Sopenharmony_ci    word-spacing: 20px;
120e41f4b71Sopenharmony_ci}
121e41f4b71Sopenharmony_ci.textstyle {
122e41f4b71Sopenharmony_ci    font-style: italic;
123e41f4b71Sopenharmony_ci}
124e41f4b71Sopenharmony_ci.textweight {
125e41f4b71Sopenharmony_ci    font-weight: 700;
126e41f4b71Sopenharmony_ci}
127e41f4b71Sopenharmony_ci.textdecoration1 {
128e41f4b71Sopenharmony_ci    text-decoration: underline;
129e41f4b71Sopenharmony_ci}
130e41f4b71Sopenharmony_ci.textdecoration2 {
131e41f4b71Sopenharmony_ci    text-decoration: line-through;
132e41f4b71Sopenharmony_ci    text-decoration-color: red;
133e41f4b71Sopenharmony_ci}
134e41f4b71Sopenharmony_ci.textalign {
135e41f4b71Sopenharmony_ci    text-align: right;
136e41f4b71Sopenharmony_ci}
137e41f4b71Sopenharmony_ci```
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci![zh-cn_image_0000001167823076](figures/js-text.png)
141e41f4b71Sopenharmony_ci
142