2

How to get the total width of a BitmapFont text?

font.draw(batch, text ,Gdx.graphics.getWidth() / 2 - /* text width */ /2,450);
  • possible duplicate of [How get a String Width in Libgdx?](http://stackoverflow.com/questions/16600547/how-get-a-string-width-in-libgdx) – bemeyer Aug 30 '15 at 09:57

1 Answers1

4

Use a GlyphLayout to calculate the width of some string using a particular font:

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

Don't put this in the render method, though. 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.

danirod
  • 340
  • 4
  • 14