1# 文本通用
2
3文本通用属性目前只针对包含文本元素的组件,设置文本样式。
4
5>  **说明:**
6>
7>  从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## fontColor
10
11fontColor(value: ResourceColor)
12
13设置字体颜色。
14
15**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
16
17**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
18
19**系统能力:** SystemCapability.ArkUI.ArkUI.Full
20
21**参数:**
22
23| 参数名 | 类型                                       | 必填 | 说明       |
24| ------ | ------------------------------------------ | ---- | ---------- |
25| value  | [ResourceColor](ts-types.md#resourcecolor) | 是   | 字体颜色。 |
26
27## fontSize
28
29fontSize(value: number | string | Resource)
30
31设置字体大小。
32
33**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
34
35**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
36
37**系统能力:** SystemCapability.ArkUI.ArkUI.Full
38
39**参数:**
40
41| 参数名 | 类型                                                         | 必填 | 说明                                                         |
42| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
43| value  | [Resource](ts-types.md#resource) \| number \| string | 是   | 字体大小。fontSize为number类型时,使用fp单位。字体默认大小16fp。不支持设置百分比字符串。 |
44
45## fontStyle
46
47fontStyle(value: FontStyle)
48
49设置字体样式。
50
51**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
52
53**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
54
55**系统能力:** SystemCapability.ArkUI.ArkUI.Full
56
57**参数:**
58
59| 参数名 | 类型                                        | 必填 | 说明                                    |
60| ------ | ------------------------------------------- | ---- | --------------------------------------- |
61| value  | [FontStyle](ts-appendix-enums.md#fontstyle) | 是   | 字体样式。<br/>默认值:FontStyle.Normal |
62
63## fontWeight
64
65fontWeight(value: number | FontWeight | string)
66
67设置文本的字体粗细,设置过大可能会在不同字体下有截断。
68
69**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
70
71**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
72
73**系统能力:** SystemCapability.ArkUI.ArkUI.Full
74
75**参数:**
76
77| 参数名 | 类型                                                         | 必填 | 说明                                                         |
78| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
79| value  | [FontWeight](ts-appendix-enums.md#fontweight)&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string | 是   | 文本的字体粗细,number类型取值[100,&nbsp;900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值的字符串形式,例如"400",以及"bold"、"bolder"、"lighter"、"regular"、"medium",分别对应FontWeight中相应的枚举值。<br/>默认值:FontWeight.Normal |
80
81## fontFamily
82
83fontFamily(value: string | Resource)
84
85设置字体列表。
86
87**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
88
89**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
90
91**系统能力:** SystemCapability.ArkUI.ArkUI.Full
92
93**参数:**
94
95| 参数名 | 类型                                                 | 必填 | 说明                                                         |
96| ------ | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
97| value  | [Resource](ts-types.md#resource)&nbsp;\|&nbsp;string | 是   | 字体列表。默认字体'HarmonyOS Sans'。<br>应用当前支持'HarmonyOS Sans'字体和[注册自定义字体](../js-apis-font.md)。<br>卡片当前仅支持'HarmonyOS Sans'字体。 |
98
99## lineHeight
100
101lineHeight(value: number | string | Resource)
102
103设置文本的文本行高,设置值不大于0时,不限制文本行高,自适应字体大小。
104
105**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
106
107**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
108
109**系统能力:** SystemCapability.ArkUI.ArkUI.Full
110
111**参数:**
112
113| 参数名 | 类型                                                         | 必填 | 说明                               |
114| ------ | ------------------------------------------------------------ | ---- | ---------------------------------- |
115| value  | [Resource](ts-types.md#resource)&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string | 是   | 文本行高,为number类型时单位为fp。 |
116
117## decoration
118
119decoration(value: DecorationStyleInterface)
120
121设置文本装饰线样式及其颜色。
122
123**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
124
125**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
126
127**系统能力:** SystemCapability.ArkUI.ArkUI.Full
128
129**参数:**
130
131| 参数名 | 类型                                                         | 必填 | 说明                                                         |
132| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
133| value  | [DecorationStyleInterface<sup>12+</sup>](ts-universal-styled-string.md#decorationstyleinterface对象说明) | 是   | 文本装饰线样式对象。<br/>默认值:<br/>{<br/>&nbsp;type:&nbsp;TextDecorationType.None,<br/>&nbsp;color:&nbsp;Color.Black,<br/>&nbsp;style:&nbsp;TextDecorationStyle.SOLID&nbsp;<br/>}<br/>**说明:** <br/>style参数不支持卡片能力。 |
134
135## 示例
136
137该示例通过fontColor、fontSize、fontStyle、fontWeight属性展示了不同样式的文本效果。
138
139```ts
140// xxx.ets
141@Entry
142@Component
143struct TextStyleExample {
144  build() {
145    Column({ space: 5 }) {
146      Text('default text')
147
148      Text('text font color red').fontColor(Color.Red)
149
150      Text('text font default')
151      Text('text font size 10').fontSize(10)
152      Text('text font size 10fp').fontSize('10fp')
153      Text('text font size 20').fontSize(20)
154
155      Text('text font style Italic').fontStyle(FontStyle.Italic)
156
157      Text('text fontWeight bold').fontWeight(700)
158      Text('text fontWeight lighter').fontWeight(FontWeight.Lighter)
159
160      Text('red 20 Italic bold text')
161        .fontColor(Color.Red)
162        .fontSize(20)
163        .fontStyle(FontStyle.Italic)
164        .fontWeight(FontWeight.Bold)
165
166      Text('Orange 18 Normal text')
167        .fontColor(Color.Orange)
168        .fontSize(18)
169        .fontStyle(FontStyle.Normal)
170    }.width('100%')
171  }
172}
173```
174
175![textstyle](figures/textstyle.png)
176