HarmonyOS开发者版主1ArkUI (方舟UI框架)
UI
HarmonyOS 5
HarmonyOS 搜索搜到的字体如何高亮? (API12+)
HarmonyOS 搜索搜到的字体如何高亮? (API12+)

行内部分文本高亮样式设置可以通过Span组件、AttributeModifier属性修改器、RichEditor组件、MutableStyledString属性字符串等实现。其中Span组件和AttributeModifier属性修改器方案主要是基于Text组件嵌套Span子组件,通过Span组件对需要高亮显示的文本单独设置高亮样式,此方案仅支持文本内容样式修改,使用简单的文本编辑场景;RichEditor组件和MutableStyledString属性字符串支持图文混排,对文本编辑能力也更加丰富,包括装饰线、段落、行为触发回调等样式,适合复杂的图文编辑场景。具体有以下四个实现方案:
方案一:
Text组件将匹配的字符串通过Span设置高亮样式。
@Entry
@Component
struct Page {
@State searchValue: string = '醉';
@State dataArr: string[] = [
'南窗枕书卧,醉眼看山横',
'海棠花下思朦胧,醉春风',
'醉里挑灯看剑,梦回吹角连营',
'醉后不知天在水,满船清梦压星河',
'指洞庭为酒,渴时浩饮,君山作枕,醉后高眠'
]
build() {
Row() {
Column() {
ForEach(this.dataArr, (item: string) => {
Row() {
Text() {
Span(item.substring(0, item.indexOf(this.searchValue)))
// 红色高亮文字
Span(this.searchValue).fontColor(Color.Red)
Span(item.substring(item.indexOf(this.searchValue) + this.searchValue.length, item.length - 1))
// 最后一个文字显示为蓝色
Span(item.substring(item.length - 1)).fontColor(Color.Blue)
}
}
.width('100%')
.height(40)
.justifyContent(FlexAlign.Center)
})
}
}
}
}
方案二:
AttributeModifier属性修改器通过applyNormalAttribute方法对Span类型组件回调,获取其属性对象并设置高亮样式。
@Entry
@Component
struct Index {
@State searchValue: string = '醉';
@State dataArr: string[] = [
'南窗枕书卧,醉眼看山横',
'海棠花下思朦胧,醉春风',
'醉里挑灯看剑,梦回吹角连营',
'醉后不知天在水,满船清梦压星河',
'指洞庭为酒,渴时浩饮,君山作枕,醉后高眠'
]
build() {
Column() {
ForEach(this.dataArr, (item: string) => {
Row() {
Text() {
// 多关键字匹配
Span(item.substring(0, item.indexOf(this.searchValue))).attributeModifier(new HighlightModifier(false))
Span(this.searchValue).attributeModifier(new HighlightModifier(true))
Span(item.substring(item.indexOf(this.searchValue) + this.searchValue.length, item.length - 1))
.attributeModifier(new HighlightModifier(false))
}
}
.height(40)
.justifyContent(FlexAlign.Center)
})
}.justifyContent(FlexAlign.Center)
}
}
export class HighlightModifier implements AttributeModifier<SpanAttribute> {
isHighlight: boolean = false
constructor(isHighlight: boolean) {
this.isHighlight = isHighlight
}
applyNormalAttribute(instance: SpanAttribute): void {
instance.fontColor(this.isHighlight ? Color.Red : Color.Black).fontSize(15)
}
}
方案三:
RichEditor组件获取匹配的字符串列表,然后通过addTextSpan方法对已匹配文本设置高亮样式。
@Entry
@Component
struct Page {
@State value: string = '醉';
@State dataArr: Tmp[] = [
new Tmp(new RichEditorController(), '南窗枕书卧,醉眼看山横'),
new Tmp(new RichEditorController(), '海棠花下思朦胧,醉春风'),
new Tmp(new RichEditorController(), '醉里挑灯看剑,梦回吹角连营'),
new Tmp(new RichEditorController(), '醉后不知天在水,满船清梦压星河'),
new Tmp(new RichEditorController(), '指洞庭为酒,渴时浩饮,君山作枕,醉后高眠'),
]
build() {
Column() {
ForEach(this.dataArr, (item: Tmp) => {
ListItem() {
Row() {
RichEditor({ controller: item.controller })
.onReady(() => {
// 通过addTextSpan添加高亮显示的属性
item.controller.addTextSpan(item.value.substring(0, item.value.indexOf(this.value)),
{
style: { fontColor: Color.Black, fontSize: 16 }
})
item.controller.addTextSpan(this.value,
{
style: { fontColor: Color.Red, fontSize: 16 }
})
item.controller.addTextSpan(item.value.substring(item.value.indexOf(this.value) + this.value.length),
{
style: { fontColor: Color.Black, fontSize: 16 }
})
})
}.width('100%').height(40).justifyContent(FlexAlign.Center)
}
})
}
}
}
class Tmp {
controller: RichEditorController
value: string
constructor(controller: RichEditorController, value: string) {
this.controller = controller
this.value = value
}
}
方案四:
使用属性字符串MutableStyledString给字符串添加高亮样式,然后通过Text组件的setStyledString方法设置其展示。
以下核心代码:
@Component
struct Page {
@State value: string = '';
@State dataArr: string[] = [
'南窗枕书卧,醉眼看山横',
'海棠花下思朦胧,醉春风',
'醉里挑灯看剑,梦回吹角连营',
'醉后不知天在水,满船清梦压星河',
'指洞庭为酒,渴时浩饮,君山作枕,醉后高眠'
]
@State showArr: Tmp[] = []
Style1: TextStyle = new TextStyle({ fontColor: Color.Red });
Style2: TextStyle = new TextStyle({ fontColor: Color.Blue });
build() {
Row() {
Column() {
Search({ value: $$this.value, placeholder: 'Type to search...' })
.onChange(() => {
this.showArr = []
this.dataArr.filter((item: string) => {
let index = item.indexOf(this.value)
// 匹配关键词,并设置对应Style样式
if (this.value && index != -1) {
let mutableStyledString = new MutableStyledString(item, [
{
start: index, length: this.value.length, styledKey: StyledStringKey.FONT, styledValue: this.Style1
},
{
start: item.length - 1, length: 1, styledKey: StyledStringKey.FONT, styledValue: this.Style2
}])
this.showArr.push(new Tmp(mutableStyledString, new TextController()))
}
})
})
ForEach(this.showArr, (item: Tmp) => {
Row() {
Text(undefined, { controller: item.controller })
.onAppear(() => {
// 通过setStyledString方法,添加对应文本样式
item.controller.setStyledString(item.style)
})
}.width('100%').height(40).justifyContent(FlexAlign.Center)
})
}
}
}
}
class Tmp {
style: MutableStyledString
controller: TextController
constructor(style: MutableStyledString, controller: TextController) {
this.style = style
this.controller = controller
}
}
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
HarmonyOS 如何加载沙盒内的ttf字体库文件(API12+)
HarmonyOS 关于h5页面无法加载注册的全局字体(API12+)
HarmonyOS 如何加载沙盒内的ttf字体库文件 (API12+)
HarmonyOS 如何获取系统字体缩放比例 (API12+)
HarmonyOS 搜索结果文本如何高亮展示(API12+)
HarmonyOS 搜索关键字高亮(API12+)
HarmonyOS 预览如何高亮显示一些文字?(API12+)
HarmonyOS Text组件显示的文本,如何让文本中个别字高亮显示(API12+)




































苏公安网备 32011402010933号