HarmonyOS鸿蒙Next中【ArkUI 文本展示系列】三:属性字符串简介,认识属性字符串
HarmonyOS鸿蒙Next中【ArkUI 文本展示系列】三:属性字符串简介,认识属性字符串 属性字符串指两个:StyledString 和 MutableStyledString,这两个组件拥有Span的所有的能力并且有所超出,并且可以非常方便地复用,给多个Text组件设置样式。
一、认识 StyledString、MutableStyledString
属性字符串的能力范围:设置文本样式,设置段落样式,设置超链接,设置手势事件,插入图片,插入自定义绘制,自定义存储。
类别 | 支持的功能 |
---|---|
字符样式 | 字体样式、装饰线、基线偏移量、字符间距、行高、背景色 |
段落样式 | 对齐方式、首行缩进、段落缩进、最大行数、超长文本显示方式、断行规则、段落间距 |
超链接 | 设置超链接 |
手势事件 | 点击、长按、触摸事件 |
插入图片 | 图片的来源、尺寸、对齐方式、缩放类型、颜色滤镜 |
自定义绘制 | 插入自定义绘制内容 |
自定义存储 | 临时存储数据 |
上面这张表格,清晰描述了StyledString的主要功能,这些功能都可以在Text、Image、Gesture、Canvas中找到类似名称的接口。
MutableStyledString继承自StyledString,在设置Text样式方面的能力和StyledString是一样的。区别是, StyledString 需要在构造时就设计好样式,而 MutableStyledString 则可以在运行时动态修改样式,对样式进行增删改查。
二、StyledString使用介绍
介绍如何通过StyledString设置Text的样式。
step1. 创建Text对象,指定controller
为什么要指定controller?因为后面需要通过controller.setStyledString()来绑定属性字符串。
@Entry
@Component
struct demo {
controller: TextController = new TextController()
build() {
Column() {
Text(undefined, { controller: this.controller })
}
}
}
step2. 创建StyledString对象
spanStyle1: SpanStyle = {
start: 0,
length: 5,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Green })
};
spanStyle2: SpanStyle = {
start: 0,
length: 2,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Red })
};
arrayStyled:Array<StyleOptions>= [this.spanStyle1,this.spanStyle2]
styledString: StyledString = new StyledString("运动45分钟",this.arrayStyled);
step3. Text绑定StyledString对象
绑定属性字符串的时机有许多约束条件。最简单的用法是在onPageShow这一生命周期阶段中绑定。
onPageShow(): void {
this.controller.setStyledString(this.styledString)
}
step4. 多个Text绑定同一个StyledString对象
属性字符串最重要的特点,就是可以多次复用。同一个属性字符串对象可以提供给多个Text组件使用。和Span的写法相比,节省重复代码。
@Entry
@Component
struct demo {
controller: TextController = new TextController()
controller2: TextController = new TextController()
controller3: TextController = new TextController()
spanStyle1: SpanStyle = {
start: 0,
length: 5,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Green })
};
spanStyle2: SpanStyle = {
start: 0,
length: 2,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Red })
};
arrayStyled: Array<StyleOptions> = [this.spanStyle1, this.spanStyle2]
styledString: StyledString = new StyledString("运动45分钟", this.arrayStyled);
onPageShow(): void {
this.controller.setStyledString(this.styledString)
this.controller2.setStyledString(this.styledString)
this.controller3.setStyledString(this.styledString)
}
build() {
Column() {
Text(undefined, { controller: this.controller })
Text(undefined, { controller: this.controller2 })
Text(undefined, { controller: this.controller3 })
}
}
}
最终效果图
三、MutableStyledString使用介绍
MutableStyledString 使用方式与StyledString一致,下面只介绍MutableStyledString在运行时动态修改样式的用法。这个例子展示了在onPageShow这一生命周期中,动态修改MutableStyledString,达到修改Text样式的效果。
step1. 创建一个MutableStyledString对象
mutableStyledString1: MutableStyledString = new MutableStyledString("");
step2. 在运行阶段,修改MutableStyledString
这里想要从this.styledString复制一份样式设置。借助appendStyledString,末尾添加了已有的属性字符串this.styledString,因为this.mutableStyledString1原本是空的,所以相当于复制。
复制之后,清空了第一个字的样式。
onPageShow(): void {
this.mutableStyledString1.appendStyledString(this.styledString) //通过append完成拷贝
this.mutableStyledString1.removeStyle(0,1,StyledStringKey.FONT) // 修改属性字符串
}
step3. 绑定给Text组件
属性字符串修改后,需要重新setStyledString,属性字符串的改动才能在对应的text上生效。
onPageShow(): void {
this.controller4.setStyledString(this.mutableStyledString1)
}
完整demo
@Entry
@Component
struct demo {
controller: TextController = new TextController()
controller2: TextController = new TextController()
controller3: TextController = new TextController()
controller4: TextController = new TextController()
spanStyle1: SpanStyle = {
start: 0,
length: 5,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Green })
};
spanStyle2: SpanStyle = {
start: 0,
length: 2,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Red })
};
arrayStyled: Array<StyleOptions> = [this.spanStyle1, this.spanStyle2]
styledString: StyledString = new StyledString("运动45分钟", this.arrayStyled);
mutableStyledString1: MutableStyledString = new MutableStyledString(""); // 创建一个空的属性字符串
onPageShow(): void {
this.mutableStyledString1.appendStyledString(this.styledString) //通过append完成拷贝
this.mutableStyledString1.removeStyle(0,1,StyledStringKey.FONT) // 修改属性字符串
this.controller.setStyledString(this.styledString)
this.controller2.setStyledString(this.styledString)
this.controller3.setStyledString(this.styledString)
this.controller4.setStyledString(this.mutableStyledString1)
}
build() {
Column() {
Text(undefined, { controller: this.controller })
Text(undefined, { controller: this.controller2 })
Text(undefined, { controller: this.controller3 })
Text(undefined, { controller: this.controller4 })
}
}
}
最终效果如下
更多关于HarmonyOS鸿蒙Next中【ArkUI 文本展示系列】三:属性字符串简介,认识属性字符串的实战教程也可以访问 https://www.itying.com/category-93-b0.html
属性字符串是ArkUI中用于描述富文本显示的核心数据结构。通过Span组件实现文本片段的差异化样式控制,支持设置字体颜色、大小、粗细、背景色等属性。
属性字符串采用声明式组合方式构建,将多个Span按需嵌套或并列组成完整文本。每个Span可独立配置样式属性,实现混合排版效果。
主要应用于需要局部样式差异的场景,如关键词高亮、多语言混排等。通过属性字符串的灵活组合,可在单一文本组件内实现复杂的视觉呈现。
更多关于HarmonyOS鸿蒙Next中【ArkUI 文本展示系列】三:属性字符串简介,认识属性字符串的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
属性字符串(StyledString和MutableStyledString)是ArkUI中强大的文本样式管理工具,相比传统的Span写法具有更丰富的功能和更好的复用性。
核心特性:
- StyledString支持字符样式(字体、颜色、背景等)、段落样式(对齐、缩进等)、超链接、手势事件、图片插入、自定义绘制等完整文本样式能力
- MutableStyledString继承自StyledString,支持运行时动态修改样式
使用优势:
- 高效复用:同一个属性字符串对象可绑定到多个Text组件,避免重复定义样式
- 样式集中管理:通过SpanStyle数组统一管理文本中各段的样式规则
- 动态更新:MutableStyledString支持appendStyledString、removeStyle等方法实时调整样式
关键使用要点:
- 必须通过TextController的setStyledString方法绑定
- 建议在onPageShow生命周期中进行绑定操作
- 修改MutableStyledString后需重新调用setStyledString使更改生效
属性字符串特别适用于需要复杂文本样式且多处复用的场景,大幅提升了代码的可维护性和开发效率。