使用RichEditor控制器添加绿色双#号文本后,键盘输入的文本颜色也是绿色,如何设置键盘输入文本的字体颜色为黑色并调整字间距?
代码如下:
onReady(() => {
this.controller.addTextSpan(`#${this.message}# `, {
style:
{ fontColor: Color.Green }
})
}) 问题效果预览:

RichEditor组件的字体样式会默认跟随最后TextSpan的样式,所以添加绿色双#号文本后,键盘输入的字体样式也是绿色。设置字间距使用style的letterSpacing属性即可。
必须添加一个空格字符(' '),不是''(引号里无空格)。
@Entry
@Component
struct RichEditorDemo {
controller: RichEditorController = new RichEditorController();
options: RichEditorOptions = { controller: this.controller };
message: string = 'Hello World';
build() {
Column() {
RichEditor(this.options)
.onReady(() => {
this.controller.addTextSpan(`#${this.message}# `, {
style:
{ fontColor: Color.Green, letterSpacing: 1 }
});
this.controller.addTextSpan(' ', {
style:
{ fontColor: Color.Black }
});
});
}.padding(16);
}
} @Entry
@Component
struct RichEditorText {
controller: RichEditorController = new RichEditorController();
options: RichEditorOptions = { controller: this.controller };
message: string = 'Hello World';
build() {
Column() {
RichEditor(this.options)
.onReady(() => {
this.controller.addTextSpan(`#${this.message}# `, {
style:
{ fontColor: Color.Green }
});
})
.aboutToIMEInput((value: RichEditorInsertValue) => {
this.controller.addTextSpan(value.insertValue, {
offset: value.insertOffset,
style: {
fontColor: Color.Black, letterSpacing: 1
}
});
return false;
});
}.padding(16);
}
}