Span 作为 Text 组件的子组件,用于显示行内文本的组件。
比如:一行文字中,只有某两个字需要大写;只有某几个字需要删除线,都需要使用 Span 将其包裹起来。
构造函数
/**
* Provide text decoration.
* @form
* @since 9
*/
interface SpanInterface {
/**
* Called when text is entered in span.
* @form
* @since 9
*/
(value: string | Resource): SpanAttribute;
}
该函数接受一个可选的参数 content,其类型为 string 或 Resource 的联合类型。
string 类型表示内容可以是一个字符串。
Resource 类型表示内容可以是一个资源对象,也就是定义在 resources 目录下的字符值资源。
函数的返回类型为 SpanAttribute,用于描述文本的样式、颜色、字体等属性。
属性
属性 | 作用 |
fontColor | 字体颜色 |
fontSize | 字号 |
fontStyle | 文本样式(斜体) |
fontWeight | 字重(粗细) |
fontFamily | 字体 |
decoration | 上划线、下划线、删除线 |
letterSpacing | 字符的间距 |
textCase | 大小写转换 |
用法
@Entry
@Component
struct Index {
@State message: string = 'Hello World ABCDEFG'
build() {
Row() {
Column() {
Text() {
Span("我已阅读并同意")
Span("《隐私协议》")
.fontColor('#0000ff')
.fontSize(20)
.fontStyle(FontStyle.Normal)
.fontWeight(FontWeight.Bold)
.decoration({ type: TextDecorationType.Underline, color: '#0000ff' })
.onClick(() => {
console.log("跳转协议链接");
})
}.fontSize(20)
}
.width('100%')
}
.height('100%')
}
}
运行结果:
当然了,这个《隐私协议》是可以点击的。