0

I have made some code in the past to check through text to find color codes that will change the color of the text. Ex:

Drawing this: Hello my name&b is john would give this as a result when drawn: Hello my name is john but "is john" would be in blue since i used the &b color code. I am basically imitating ansi but for java.

Now I would like to wrap the text exactly like LibGDX's BitMapFont.drawWrapped(...) but still use the color codes. Anyone know how to do this? I would probobly be able to figure it out eventually but I just wanted to know if there was a better way than mine.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
vedi0boy
  • 1,030
  • 4
  • 11
  • 32

1 Answers1

1

There is support for color markup: Color Markup Language:

The BitmapFontCache class supports in-string colored text through a simple markup language.

Markup is disabled by default. Use the method BitmapFont.setMarkupEnabled() to turn it on/off.

The markup syntax is really simple but still versatile:

  • [name] Sets the color by name. There are a few predefined colors, see the Colors.reset() method for an exhaustive list. Users can define their own colors through the methods of the Colors class.
  • [#xxxxxxxx] Sets the color specified by the hex value xxxxxxxx in the form RRGGBBAA where AA is optional and defaults to 0xFF.
  • [] Sets the color to the previous color (kind of optional end tag)
  • [[ Escapes the left bracket.

Notice that color names are case-sensitive, cannot be empty, cannot start with neither # nor [, and cannot contain ]. Also, any occurrence of [ in the color name must not be escaped.

For a sample code see the test class BitmapFontTest.

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • wow thanks, instead of changing the color of the bitmap font, i have decided to just replace the color codes (&r, &b, etc) with the libgdx codes. Thanks a ton! Also can u change whether the text is bold or not? – vedi0boy Oct 31 '14 at 14:45
  • Nevermind doesn't really, matter, i decided not to use this finally. I'm working on an mmo and if people write an invalid hex code in chat it will make everyones game crash. I think I had something good going before. Thanks anyways! – vedi0boy Oct 31 '14 at 15:23