简体   繁体   中英

How to figure out the width and height of a written bitmap font string

Okay, so I have this.

calibri.draw(FBLA_Game.spriteBatch, "Rooftop Defender",0 ,0 );

Anyways, this code... I need the text to be centered. Instead of the 0's, I would have it like this:

calibri.draw(FBLA_Game.spriteBatch, "Rooftop Defender",Gdx.graphics.getWidth()/2 ,Gdx.graphics.getHeight()/2 );

But this isn't what I want. this centers the beginning of where it writes the words. So then for the width I would need to subtract the width of the word being written, But I cannot figure out how to get the width of "Rooftop Defender" that is being written.

Thanks for any and all help, Alex

In new versions ( >1.6 ) of LibGDX BitmapFont is being handled with GlyphLayout class which you can use to calculate the future width of your rendered string

    //out of render() method! this call is expensive
    GlyphLayout glyphLayout = new GlyphLayout(calibri, "Rooftop Defender");

    ...

    //render()
    float glyphLayoutWidth = glyphLayout.width;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM