简体   繁体   English

libgdx如何获取一个文本的总宽度? (BitmapFont)

[英]Libgdx how to get the total width of a text? (BitmapFont)

如何获得BitmapFont文本的总宽度?

font.draw(batch, text ,Gdx.graphics.getWidth() / 2 - /* text width */ /2,450);

Use a GlyphLayout to calculate the width of some string using a particular font: 使用GlyphLayout使用特定字体计算某些字符串的宽度:

GlyphLayout layout = new GlyphLayout(font, text);
float textWidth = layout.width;

Don't put this in the render method, though. 但是不要将其放在render方法中。 It is a good idea to calculate and store this value once in create or show and then just use the value in your render . 最好在createshow一次计算并存储该值,然后在render使用该值,这是一个好主意。

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

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