20

I have been trying to figure out a way to center text on a button, but can't find an easy, multi-purpose way to. I can do it, but it will only work for a certain string, not for any string. i would like to know if there is a way to center any string on a button. My button in this case is 185x50.

I have been able to center this button on the screen, like so:

buttonX = WIDTH / 2 - (screen.getRegionWidth() / 2);
buttonY = HEIGHT / 2 - (screen.getRegionHeight() / 2);

Any help would be much appreciated. :)

Sean Heiss
  • 780
  • 2
  • 9
  • 25

3 Answers3

61

Updated the answer to libgdx version 1.7.1-SNAPSHOT:

The easiest way to do this, is to use the TextButton class from libgdx. The text from a TextButton is centered by default. This still works!

Updated example:

final BitmapFont font = new BitmapFont();

final String text = "Test";

final GlyphLayout layout = new GlyphLayout(font, text);
// or for non final texts: layout.setText(font, text);

final float fontX = objectX + (objectWidth - layout.width) / 2;
final float fontY = objectY + (objectHeight + layout.height) / 2;

font.draw(batch, layout, fontX, fontY);

Outdated example:

This no longer works!

If you do not want to use it, you can get the font width and height with:

    font.getBounds("Test text");

So you can do something like this:

    String fontText = "";
    fontX = buttonX + buttonWidth/2 - font.getBounds(fontText).width/2;
    fontY = buttonY + buttonHeight/2 + font.getBounds(fontText).height/2;
Pinkie Swirl
  • 2,375
  • 1
  • 20
  • 25
  • 2
    Just to add to this answer, you can easily center by just subtracting the width of the text from the game's width and dividing by 2. `fontX = (gameWidth - font.getBounds(fontText).width) / 2` – Willem Ellis Dec 24 '14 at 15:37
  • I think that they changed it cause I can't find it in api – Alex K Apr 21 '15 at 15:50
  • 1
    @AlexKapustian you can use glyphlayout (in the newer version of the libgdx) for this purpose. – bytestorm May 22 '15 at 07:27
  • Why all this worry about centering text arbitrarily? Just use TextButton instead. It automatically centers its text – Malkierian Jul 30 '15 at 21:16
15

For the newer version of libgdx the function BitMapFont.getBounds() isn't there in api anymore. You can use GlyphLayout to get bounds.For example,

BitmapFont font;
SpriteBatch spriteBatch;
//... Load any font of your choice first
FreeTypeFontGenerator fontGenerator = new FreeTypeFontGenerator(
   Gdx.files.internal("myFont.ttf")
);
FreeTypeFontGenerator.FreeTypeFontParameter freeTypeFontParameter =
      new FreeTypeFontGenerator.FreeTypeFontParameter();
freeTypeFontParameter.size = size;
fontGenerator.generateData(freeTypeFontParameter);
font = fontGenerator.generateFont(freeTypeFontParameter);

//Initialize the spriteBatch as requirement

GlyphLayout glyphLayout = new GlyphLayout();
String item = "Example";
glyphLayout.setText(font,item);
float w = glyphLayout.width;
font.draw(spriteBatch, glyphLayout, (Game.SCREEN_WIDTH - w)/2, y);
bytestorm
  • 1,411
  • 3
  • 20
  • 36
  • The TextButton that Pinkie Swirl alluded to was a non-starter without using `stage`. Good to see `setText` used instead of needless instantiation. – John Apr 22 '18 at 20:12
-1

Try the following:

label.setPosition(Gdx.graphics.getWidth()/2-(label.getPrefWidth()/2),Gdx.graphics.getHeight()-(label.getPrefHeight()/2));
d219
  • 2,707
  • 5
  • 31
  • 36