TextController显示属性字符串 HarmonyOS 鸿蒙Next
TextController显示属性字符串 HarmonyOS 鸿蒙Next https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-styled-string-V5#customspan
TextController显示属性字符串 如何给自定义的CustomSpan添加点击回调
用官方的CustomSpan添加GestureStyle 点击事件不响应
更多关于TextController显示属性字符串 HarmonyOS 鸿蒙Next的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你是不是没有设置styledKey为StyledStringKey.GESTURE,可以参考以下demo
import { common2D, drawing } from '@kit.ArkGraphics2D'
export class MyCustomSpan extends CustomSpan {
width: number = 160;
word: string = "drawing";
height: number = 10;
constructor(word: string, width: number, height: number) {
super();
this.word = word;
this.width = width;
this.height = height;
}
onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics {
return { width: this.width, height: this.height };
}
onDraw(context: DrawContext, options: CustomSpanDrawInfo) {
let canvas = context.canvas;
const pen = new drawing.Pen();
pen.setColor({ alpha: 255, red: 0, green: 74, blue: 175 });
const font = new drawing.Font();
font.setSize(80);
const textBlob = drawing.TextBlob.makeFromString(this.word, font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
canvas.attachPen(pen);
const rect: common2D.Rect = {
left: options.x + 10,
right: options.x + vp2px(this.width) - 10,
top: options.lineTop + 10,
bottom: options.lineBottom - 10
}
let roundRect = new drawing.RoundRect(rect, 10, 10);
path.addRoundRect(roundRect, drawing.PathDirection.CLOCKWISE)
pen.setColor({ alpha: 255, red: 23, green: 169, blue: 141 });
canvas.attachPen(pen); canvas.drawPath(path)
canvas.drawTextBlob(textBlob, options.x + 20, options.lineBottom - 15);
canvas.detachPen();
}
setWord(word: string) {
this.word = word;
}
}
import { MyCustomSpan } from './MyCustomSpan';
@Entry
@Component
struct Index {
customSpan: MyCustomSpan = new MyCustomSpan("123456", 80, 10);
style1: MutableStyledString = new MutableStyledString(this.customSpan);
textController: TextController = new TextController();
isPageShow: boolean = true;
aboutToAppear(): void {
}
build() {
Row() {
Button('加载自定义span').onClick(() =>{
this.style1.appendStyledString(
new MutableStyledString("文本绘制 示例代码 CustomSpan",
[
{
start: 0,
length: 5,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Pink, }),
},
{
start: 0,
length: 5,
styledKey: StyledStringKey.GESTURE,
styledValue: new GestureStyle({
onClick:(e)=>{
console.log('点击事件123')
}
}),
},
{ start: 5,
length: 5,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Orange, fontStyle: FontStyle.Italic }) },
{ start: 10,
length: 500,
styledKey: StyledStringKey.FONT,
styledValue: new TextStyle({ fontColor: Color.Green, fontWeight: FontWeight.Bold })
}
]
))
this.textController.setStyledString(this.style1);
})
Text(undefined, { controller: this.textController });
}.width('100%')
}
}
更多关于TextController显示属性字符串 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
但是这个似乎只是对后拼接的文字“文本绘制 ”添加了交互响应,我的目的是让MyCustomSpan “123456” 可以点击交互,
在HarmonyOS鸿蒙Next中,TextController
用于控制文本的显示和操作。TextController
可以显示属性字符串,通过设置其text
属性来展示带有样式的文本。属性字符串可以通过TextSpan
和TextStyle
来定义,支持设置字体、颜色、背景色等样式属性。TextController
还提供了对文本内容进行增删改查的方法,允许动态更新显示的文本内容。在鸿蒙Next中,TextController
与UI组件如Text
结合使用,能够实现复杂的文本显示效果。