Multi-colored text in libgdx(libgdx 中的多色文本)
问题描述
我发现 LibGDX 在夜间构建中有一个新组件 - TextArea
,它是 scene2d.ui
包的一部分.很高兴有这样的组件,非常易于使用,但我缺少的是对多色文本的一些支持.
I found out there is a new component in LibGDX in nightly builds - TextArea
which is part of the scene2d.ui
package. It's nice to have a component like this, very easy to use, but what I'm missing is some support for a multi-colored text.
我想用不同的颜色突出显示文本中的一些关键字,但我不知道如何使用当前的 api 来实现.BitmapFontCache 类中有一种方法:
I want to highlight some keywords in a text with a different color but I don't know how to do it with current api. There is one method in BitmapFontCache class:
public void setColors (Color tint, int start, int end)
此方法的 Javadoc 说明如下:
Javadoc for this method says following:
设置指定字符的颜色.这只能在 setText(CharSequence, float, float) 之后调用,并且每次调用 setText 时都会重置.
Sets the color of the specified characters. This may only be called after setText(CharSequence, float, float) and is reset every time setText is called.
但我不知道如何通过 TextArea 对象使用它,或者是否可以这样做.有人试图弄清楚吗?我们将不胜感激每一个提示.
But I don't know how to use it through TextArea object or if it's even possible to do it that way. Someone who tried to figure it out? Every hint will be appreciated.
推荐答案
Libgdx 提供颜色标记,必须先在 BitmapFont 上启用
Libgdx offers color markup, which must first be enabled on the BitmapFont with
font.getData().markupEnabled = true;
使用该字体呈现的文本将查找 颜色标记,其中颜色用括号括起来.每种使用的颜色都被压入堆栈.
Text rendered with that font will look for color markup, where colors are surrounded in brackets. Each used color is pushed onto a stack.
- 命名颜色(区分大小写):
[RED]red [ORANGE]orange
- 带有可选 alpha 的十六进制颜色:
[#FF0000]red [#FF000033]transparent
- 一组空括号从堆栈中弹出一种颜色:
[BLUE]Blue text[RED]Red text[]Blue text
- 双括号
[[
表示转义的括号字符,但如果后跟右括号,它将无法按预期工作.
- Named colors (case sensitive):
[RED]red [ORANGE]orange
- Hex colors with optional alpha:
[#FF0000]red [#FF000033]transparent
- A set of empty brackets pops a color off the stack:
[BLUE]Blue text[RED]Red text[]Blue text
- A double bracket
[[
represents an escaped bracket character, however it will not work as expected when followed by a closing bracket.
命名颜色在类中定义 com.badlogic.gdx.graphics.Colors,可以用Colors.put("NAME", color);
添加.
Named colors are defined in the class com.badlogic.gdx.graphics.Colors, and can be added with Colors.put("NAME", color);
.
这篇关于libgdx 中的多色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:libgdx 中的多色文本
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01