4

I am new to Libgdx but with proficient knowledge in Android. I am able to place the texture in center of the screen. But when it comes to bitmapfont with the string "Level 1", I am not able to decide the width of the String and hence I am not able to place it in center of the screen.

Could you please help me to find the width/height of the Bitmapfont which has the string "Level 1"

iappmaker
  • 2,945
  • 9
  • 35
  • 76
  • 4
    possible duplicate of [How get a String Width in Libgdx?](http://stackoverflow.com/questions/16600547/how-get-a-string-width-in-libgdx) – dawez Oct 13 '14 at 16:17

2 Answers2

9

font.getBounds() doesn't work anymore, so the answer should be updated.

You can use the following:

GlyphLayout glyphLayout = new GlyphLayout();
String item = "Example";
glyphLayout.setText(font,item);
float w = glyphLayout.width;

and use w to center with respect to a position

Buddy
  • 2,074
  • 1
  • 20
  • 30
0

You can get the width of a string drawn with a Bitmapfont this way:

font.getBounds(<String>).width;

To draw it in the center of your screen (using a camera) would be like this:

int level = 1;
String s = "Level "+level;
font.draw(batch, s, camera.viewportWidth/2F-font.getBounds(s).width/2F, camera.viewportHeight()/2F);
Daahrien
  • 10,190
  • 6
  • 39
  • 71