Scaling and positioning text in libgdx

I am working with libgdx. I need to scale and place text. Suppose I want to draw an X with a height of 30px and I want it to be in the middle of the screen. I want to attract more people who are in different places and at different scales.

Is there a way how I can achieve this? I cannot find a solution anywhere. I don't want to create more BitmapFonts if possible.

+3


source to share


1 answer


If you want to handle all platforms (android, html, ios, desktop) you need to use multiple BitmapFonts to avoid ugly scaling. Otherwise, if you don't need to deploy to HTML, you can use the gdx-freetype extension (see https://github.com/libgdx/libgdx/wiki/Gdx-freetype here ).

Assuming you are going with a BitmapFont, you can simply use code like this to center your text:



        String text = "Your text here!";
        TextBounds bounds = font.getBounds(text);
        font.draw(batch, text, (width - bounds.width) / 2.0f, (height - bounds.height) / 2.0f);

      

For scaling, you can set the scale in font.draw, but you probably want multiple BitmapFonts of different sizes to avoid ugly artifacts.

+1


source







All Articles