LibGDX - FreeTypeFont Centering

Hi there,

I’m currently using the FreeTypeFontGenerator for fonts. I’m looking for a way to center the text at all times, no matter the text or score.

Example:

    • 70 - -
  • 700 -
  • 7000 -
    -70000-
    … etc… if that makes sense.

Thanks!


BitmapFont font;
String s;

float width = f.getBounds(s).width;

// center text horizontally at (x,y)
draw(s, x - width / 2,  y); // something like this

Alternatively experiment with
drawWrapped(Batch batch, java.lang.CharSequence str, float x, float y, float wrapWidth, BitmapFont.HAlignment alignment)
and just set wrap width high enough. This is in http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html

Or if you’re using a monospaced font you can pad with spaces like you did in the OP. That’s a giant hack though.