如何在 TextField for Harmony OS 中设置文本的颜色?

How to set the color of a text in TextField for Harmony OS?

如何设置TextField中文本的颜色?例如,将“Hello Harmony”字段中的“Harmony”字体设置为红色 Android 代码实现如下:

SpannableStringBuilder ssb = new SpannableStringBuilder(); ssb.setSpan(新 ForegroundColorSpan(getCurrentHintTextColor()), i, i + 1, 0);

可以使用RichText for Harmony OS,可以达到同样的效果。请参阅下面的示例代码截图,它将 Harmony 设置为红色。

Text text = ComponentHelper.getShapeText( context: this); 
text.setTextSize(80); 

RichTextBuilder builder = new RichTextBuilder); 

TextForm redForm = new TextForm(); 
redForm.setTextColor(Color.RED.getValue(0);  
redForm.setTextSize(50); 
builder.mergeForm(redForm); 
builder.addText("Harmony"); 

TextForm blackForm = new TextForm(); 
blackForm.setTextColor (Color.BLACK.getValue ()); 
blackForm.setTextSize(60); 
builder.mergeForm(blackForm); 
builder.addText("OS"); 
text.setRichText(builder.build());

RichTextBuilder 和 TesxForm 链接如下:

https://developer.harmonyos.com/en/docs/documentation/doc-references/richtextbuilder-0000001054358740

https://developer.harmonyos.com/en/docs/documentation/doc-references/textform-0000001054120081