How to Change the Text Color in a String to Multiple Colors in Java

Is it possible to change the text color in a string to multiple colors in Java?

Yes, its possible. For this you need to use SpannableString and ForegroundColorSpan.

This should look something like this:

SpannableStringBuilder builder = new SpannableStringBuilder();

String red = "this is red";
SpannableString redSpannable= new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);

String white = "this is white";
SpannableString whiteSpannable= new SpannableString(white);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0);
builder.append(whiteSpannable);

String blue = "this is blue";
SpannableString blueSpannable = new SpannableString(blue);
blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
builder.append(blueSpannable);

mTextView.setText(builder, BufferType.SPANNABLE);

Sample Image

Setting multiple text colors in a String in a JTextArea

No. JTextArea cannot be used for that. It supports only and only plain text. Whatever HTML you write in it, will appear as it is.

As an alternative, you can either use JTextPane or JEditorPane.

Check docs:

JTextPane: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html

JEditorPane: http://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html

How do I create text with different colors

No, there is no such method.

You will have to split it up like:

g.setColor(Color.blue);
g.drawString("Hi,", 50, 50);

g.setColor(Color.red);
g.drawString("Stackoverflow!", 50 + font.getWidth("Hi, "), 50);

What you can do as a workaround is to write yourself a helper method that does all the calculation and handling of the offsets if you need it more often. Something like:

public void drawString(Graphics g, int x, int y, Object... args)
{
org.newdawn.slick.Font font = g.getFont();
int currentOffset = x;
int startOffset = x;

for (int i = 0; i < args.length / 2; i++)
{
g.setColor((Color) args[i * 2]);
g.drawString((String) (args[i * 2 + 1]), currentOffset, y);

currentOffset += font.getWidth(((String) (args[i * 2 + 1])));

if (((String) (args[i * 2 + 1])).contains("\n"))
{
currentOffset = startOffset;
}
}
}

And call it like:

drawString(g, 10, 10, "Stack", Color.red, "Overflow", Color.green, "!", Color.yellow);

How can I dynamically change the font colour within a JTextArea?

A JTextArea is only meant to contain plain text and cannot color certain words. If you want to be able to color different words, you need to use a JTextPane or a JEditorPane.

For more information, see this question. This question might also be helpful (especially the second answer).

Here is an example:

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

Style style = textPane.addStyle("Style", null);
StyleConstants.setForeground(style, Color.red);
String word = "Hello";

if (word.equals("Hello")) {
try {
doc.insertString(doc.getLength(), word, style);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
} else {
StyleConstants.setForeground(style, Color.blue);

try {
doc.insertString(doc.getLength(), word, style);
} catch (BadLocationException e) {
e.printStackTrace();
}
}

This makes a String word. If word is "Hello" it will be displayed in red, otherwise it will be displayed in blue.

Change color of two parts of string when using String.format()

I did it. What I did was find the numbers using regex and used
Spannable in the while loop of the matches.find() to color the index of the matches. The start was the index and the end index + 2.

How to set two different colors for a single string in itext

First this: you are using an obsolete version of iText. Please upgrade!

As for your question: a Paragraph consists of a series of Chunk objects. A Chunk is an atomic part of text in which all the glyphs are in the same font, have the same font size, color, etc...

Hence you need to split your String in two parts:

Font dataGreenFont = FontFactory.getFont("Garamond", 10, BaseColor.GREEN);
Font dataBlackFont = FontFactory.getFont("Garamond", 10, BaseColor.BLACK);
Paragraph p = new Paragraph();
p.Add(new Chunk("Developed By : ", dataGreenFont));
p.Add(new Chunk("Mr.XXXXX", dataBlackFont));
document.add(p);

Single TextView with multiple colored text

yes, if you format the String with html's font-color property then pass it to the method Html.fromHtml(your text here)

String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));


Related Topics



Leave a reply



Submit