How to get BitmapFont width in LibGDX?

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

  • How to get the total width of a BitmapFont text? font.draw(batch, text ,Gdx.graphics.getWidth() / 2 - /* text width */ /2,450);

  • Answer:

    Use a https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/GlyphLayout.html 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.

Mr Zombie Pigman at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.