How to set the color of a text in TextField? For example, set the "Harmony" font in the "Hello Harmony" field to red The Android code is implemented as follows:
SpannableStringBuilder ssb = new SpannableStringBuilder(); ssb.setSpan(new ForegroundColorSpan(getCurrentHintTextColor()), i, i + 1, 0);
Can use RichText for Harmony OS, you can achieve the same effect. Please see below screenshot for the sample code, which will set Harmony to red.
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 and TextForm Links are below:
https://developer.harmonyos.com/en/docs/documentation/doc-references/textform-0000001054120081