简体   繁体   English

LibGDX WrappedText,但使用文本内的颜色(java)

[英]LibGDX WrappedText but by using colors within the text (java)

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. 绘制此: Hello my name&b is john绘制时会给出以下结果: Hello my name is john但由于我使用了&b颜色代码,所以“ is john”将为蓝色。 I am basically imitating ansi but for java. 我基本上是模仿ansi,但对于Java。

Now I would like to wrap the text exactly like LibGDX's BitMapFont.drawWrapped(...) but still use the color codes. 现在,我想像LibGDX的BitMapFont.drawWrapped(...)一样完全包装文本,但仍使用颜色代码。 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. 我可能最终能弄清楚,但我只是想知道是否有比我更好的方法。

There is support for color markup: Color Markup Language : 支持颜色标记: 颜色标记语言

The BitmapFontCache class supports in-string colored text through a simple markup language. BitmapFontCache类通过简单的标记语言支持字符串中的彩色文本。

Markup is disabled by default. 默认情况下禁用标记。 Use the method BitmapFont.setMarkupEnabled() to turn it on/off. 使用方法BitmapFont.setMarkupEnabled()将其打开/关闭。

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. 有一些预定义的颜色,请参见Colors.reset()方法以获取详尽的列表。 Users can define their own colors through the methods of the Colors class. 用户可以通过Colors类的方法定义自己的颜色。
  • [#xxxxxxxx] Sets the color specified by the hex value xxxxxxxx in the form RRGGBBAA where AA is optional and defaults to 0xFF. [#xxxxxxxx]以RRGGBBAA格式设置由十六进制值xxxxxxxx指定的颜色,其中AA是可选的,默认为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 . 有关示例代码,请参见测试类BitmapFontTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM