本文介绍了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 时都会重置.

但我不知道如何通过 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 中的多色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:33