• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Span
2
3As a child of the [\<Text>](ts-basic-components-text.md) and [\<ContainerSpan>](ts-basic-components-containerspan.md) components, the \<Span> component is used to display inline text.
4
5>  **NOTE**
6>
7>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8>
9>  Since API version 10, this component can inherit the attributes of the **\<Text>** parent component. That is, if no attribute is set for this component, it inherits the attributes (if set) of its parent component. Only the following attributes can be inherited: **fontColor**, **fontSize**, **fontStyle**, **fontWeight**, **decoration**, **letterSpacing**, **textCase**, **fontfamily**, and **textShadow**.
10
11
12## Child Components
13
14Not supported
15
16
17## APIs
18
19Span(value: string | Resource)
20
21Since API version 9, this API is supported in ArkTS widgets.
22
23**Parameters**
24
25| Name| Type| Mandatory| Description|
26| -------- | -------- | -------- | -------- |
27| value | string \| [Resource](ts-types.md#resource) | Yes| Plain text.|
28
29
30## Attributes
31
32Only the [universal text attributes](ts-universal-attributes-text-style.md) are supported.
33
34| Name| Type| Description|
35| -------- | -------- | -------- |
36| decoration | {<br>type: [TextDecorationType](ts-appendix-enums.md#textdecorationtype),<br>color?: [ResourceColor](ts-types.md#resourcecolor)<br>} | Style and color of the text decorative line.<br>Default value: {<br>type: TextDecorationType.None<br>color: Color.Black<br>} <br>Since API version 9, this API is supported in ArkTS widgets.|
37| letterSpacing       | number \| string  | Letter spacing. A negative value tightens the spacing; a positive value loosens the spacing, and the letters are spread farther apart with the value.<br>Since API version 9, this API is supported in ArkTS widgets.                               |
38| textCase | [TextCase](ts-appendix-enums.md#textcase) | Text case.<br>Default value: **TextCase.Normal**<br>Since API version 9, this API is supported in ArkTS widgets.|
39| font<sup>10+</sup> | [Font](ts-types.md#font) | Text style, covering the font size, font width, Font family, and font style.|
40| textShadow<sup>11+</sup>  |  [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) \| Array&lt;[ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions)> | Text shadow. It supports input parameters in an array to implement multiple text shadows.<br>**NOTE**<br>This API does not work with the **fill** attribute or coloring strategy.|
41| textBackgroundStyle<sup>11+</sup> | [TextBackgroundStyle](ts-basic-components-containerspan.md#textbackgroundstyle)                                                                                           | Background style.<br>Default value:<br>{<br>  color: Color.Transparent,<br>  radius: 0<br>} <br>**NOTE**<br>This attribute prioritizes the value separately set for the component. If it is not set, the component can inherit the settings from its parent [\<ContainerSpan>](ts-basic-components-containerspan.md).|
42
43
44## Events
45
46Among all the universal events, only the [click event](ts-universal-events-click.md) is supported.
47
48>  **NOTE**
49>
50>  As the **\<Span>** component does not include size information, the **target** attribute of the **ClickEvent** object returned by the click event is invalid.
51
52
53## Example
54### Example 1
55```ts
56// xxx.ets
57@Entry
58@Component
59struct SpanExample {
60  build() {
61    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
62      Text('Basic Usage').fontSize(9).fontColor(0xCCCCCC)
63      Text() {
64        Span('In Line')
65        Span(' Component')
66        Span(' !')
67      }
68
69      Text() {
70        Span('This is the Span component').fontSize(12).textCase(TextCase.Normal)
71          .decoration({ type: TextDecorationType.None, color: Color.Red })
72      }
73
74      // Add a line under the text.
75      Text('Text Decoration').fontSize(9).fontColor(0xCCCCCC)
76      Text() {
77        Span('I am Underline-span').decoration({ type: TextDecorationType.Underline, color: Color.Red }).fontSize(12)
78      }
79
80      Text() {
81        Span('I am LineThrough-span')
82          .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
83          .fontSize(12)
84      }
85
86      Text() {
87        Span('I am Overline-span').decoration({ type: TextDecorationType.Overline, color: Color.Red }).fontSize(12)
88      }
89
90      // Set the letter spacing.
91      Text('LetterSpacing').fontSize(9).fontColor(0xCCCCCC)
92      Text() {
93        Span('span letter spacing')
94          .letterSpacing(0)
95          .fontSize(12)
96      }
97
98      Text() {
99        Span('span letter spacing')
100          .letterSpacing(-2)
101          .fontSize(12)
102      }
103
104      Text() {
105        Span('span letter spacing')
106          .letterSpacing(3)
107          .fontSize(12)
108      }
109
110
111      // Set the text case.
112      Text('Text Case').fontSize(9).fontColor(0xCCCCCC)
113      Text() {
114        Span('I am Lower-span').fontSize(12)
115          .textCase(TextCase.LowerCase)
116          .decoration({ type: TextDecorationType.None })
117      }
118
119      Text() {
120        Span('I am Upper-span').fontSize(12)
121          .textCase(TextCase.UpperCase)
122          .decoration({ type: TextDecorationType.None })
123      }
124    }.width('100%').height(250).padding({ left: 35, right: 35, top: 35 })
125  }
126}
127```
128
129![Span](figures/span.png)
130
131### Example 2
132``` ts
133@Entry
134@Component
135struct TextSpanExample {
136  @State textShadows : ShadowOptions | Array<ShadowOptions> = [{ radius: 10, color: Color.Red, offsetX: 10, offsetY: 0 },{ radius: 10, color: Color.Black, offsetX: 20, offsetY: 0 },
137      { radius: 10, color: Color.Brown, offsetX: 30, offsetY: 0 },{ radius: 10, color: Color.Green, offsetX: 40, offsetY: 0 },
138      { radius: 10, color: Color.Yellow, offsetX: 100, offsetY: 0 }]
139  build() {
140    Column({ space: 8 }) {
141      Text() {
142        Span('123456789').fontSize(50).textShadow(this.textShadows)
143      }
144      Text() {
145        Span('123456789') // span can inherit text shadow & font size from outer text
146      }.fontSize(50).textShadow(this.textShadows)
147    }
148  }
149}
150```
151![TextshadowExample](figures/text_span_textshadow.png)
152
153### Example 3
154``` ts
155// xxx.ets
156@Component
157@Entry
158struct Index {
159  build() {
160    Column() {
161      Text() {
162        Span('   Hello World !   ')
163          .fontSize('20fp')
164          .textBackgroundStyle({color: "#7F007DFF", radius: "5vp"})
165          .fontColor(Color.White)
166      }
167    }.width('100%').margin({bottom: '5vp'}).alignItems(HorizontalAlign.Center)
168  }
169}
170```
171![TextBackgroundStyleExample](figures/span_textbackgroundstyle.png)
172