0

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

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

4

1 回答 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

于 2021-04-21T16:31:08.723 回答