libgdx中的彩色文本 [英] Multi-colored text in libgdx

查看:96
本文介绍了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中的彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆