libGDX 中的 TrueType 字体 [英] TrueType Fonts in libGDX

查看:29
本文介绍了libGDX 中的 TrueType 字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我如何在 libGDX 中使用 TTF 字体?我环顾四周,看到了有关 StbTrueTypeFont 的内容,但它似乎不在最新版本中.

Does anyone know how I can use a TTF font in libGDX? I have looked around and have seen things about StbTrueTypeFont but it doesn't seem to be in the latest release.

我找到了 StbTrueType 字体的东西,jar 文件位于扩展目录中.我已将其添加到我的项目中.现在我只需要弄清楚如何使用它.有什么例子吗?

I found the StbTrueType font stuff, the jar file is located in the extensions directory. I've added it to my project. Now I just need to figure out how to use it. Any examples?

推荐答案

是的,您肯定需要将 gdx-stb-truetype jar 添加到您的项目中,如您在编辑中所述.这是您将如何使用它,非常简单...

Yes you will definitely need to add the gdx-stb-truetype jars to your project as you stated in your edit. Here is how you will use it, pretty straighforward...

首先你需要声明你的 BitmapFont 和你将使用的字符...

First you need to declare your BitmapFont and the characters you will use...

BitmapFont font;
public static final String FONT_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789][_!$%#@|\/?-+=()*&.;,{}"´`'<>";

然后你需要创建字体...

Then you need to create the font...

font = TrueTypeFontFactory.createBitmapFont(Gdx.files.internal("font.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
font.setColor(1f, 0f, 0f, 1f);

您可以使用传递给 createBitmapFont() 的参数,您将看到它们的作用.

You can play with the arguments you pass to createBitmapFont() and you will see what they do.

然后渲染字体,你会像往常一样做...

Then to render the font you would do it as you normally do...

batch.begin();
font.draw(batch, "This is some text", 10, 10);
batch.end();

这篇关于libGDX 中的 TrueType 字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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