简体   繁体   English

如何在TextButton LibGDX中更改TrueTypeFont的字体颜色

[英]How to change the font color of a TrueTypeFont in a TextButton LibGDX

I am trying to create a TextButton inside my application and I am using TrueType fonts(.ttf), but when I change the color of the font using rgba format with 我正在尝试在应用程序内部创建TextButton,并且正在使用TrueType字体(.ttf),但是当我使用rgba格式更改字体颜色时,

 TextButtonStyle.fontColor = [a color];

,the color set by LibGDX is not the one I intended. ,LibGDX设置的颜色不是我想要的颜色。 I think the problem is caused by the fact that the font already has a color set in its parameters and it is interfering with the one I am setting. 我认为问题是由于字体的参数中已经设置了颜色,并且干扰了我正在设置的颜色。 How could I fix that? 我该如何解决?

Set white color in FreeTypeFontParameter 's attribute and required color in Style. FreeTypeFontParameter的属性中设置白色,并在“样式”中设置所需的颜色。

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("xyz.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.color = Color.WHITE;

You must set the r, g, b and alpha component as floats in the range [0,1]. 您必须将r,g,b和alpha分量设置为[0,1]范围内的浮点数。

The code Abhishek provided is going to work fine and if you wanna provide your own custom color you can do it like this: Abhishek提供的代码可以正常工作,如果您想提供自己的自定义颜色,则可以这样做:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myFont.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.color = new Color(235/255f,73/255f,78/255f,1);

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

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