Multicolor text in libgdx

I found out that in LibGDX there is a new component in nightly builds - TextArea , which is part of the scene2d.ui package. It's nice to have such a component that is very easy to use, but what I am missing is support for multicolor text.

I want to highlight some keywords in the text with a different color, but I do not know how to do this with the current api. There is one method in the BitmapFontCache class:

 public void setColors (Color tint, int start, int end) 

The javadoc for this method says the following:

Sets the color of the specified characters. This can only be called after setText (CharSequence, float, float) and reset with every call to setText.

But I do not know how to use it through the TextArea object, or if it is possible to do so. Someone who was trying to figure this out? Every hint will be appreciated.

+5
java libgdx
source share
2 answers

Libgdx offers color markup that must first be enabled on BitmapFont using

 font.getData().markupEnabled = true; 

Text obtained with this font will search for color markup , where the colors are enclosed in square brackets. Each color used is pushed onto the stack.

  • Named colors (case sensitive): [RED]red [ORANGE]orange
  • Hex colors with optional alpha: [#FF0000]red [#FF000033]transparent
  • A set of empty brackets gives the color from the stack: [BLUE]Blue text[RED]Red text[]Blue text
  • The double bracket [[ is a shielded bracket character, however, it will not work as expected after closing the closing bracket.

Named colors are defined in the class com.badlogic.gdx.graphics.Colors and can be added using Colors.put("NAME", color); .

+3
source share

Hope it's not too late.

I have not tried this in my own way, but I swear that you will have to overwrite the setText method and then set the colors for the specific points that you want. start and end are indexes for the pieces of text you want in this particular color.

I have implemented MulticolorTextArea here: https://github.com/AnEmortalKid/MulticolorTextArea/tree/mta-release

Hope this helps.

+1
source share

All Articles