How get a string width in Libgdx?

后端 未结 3 779
一整个雨季
一整个雨季 2020-12-01 09:06

I wonder know how to get the width of my string in pixels

3条回答
  •  攒了一身酷
    2020-12-01 09:58

    According to @Nates answer: https://stackoverflow.com/a/20759876/619673 calling method

    BitmapFont.getBounds(String str).width
    

    not always returns proper width! Especially when you are reusing font. If you want draw text for example in the center of part of view port, you can avoid this problem by using another method

    BitmapFont.drawWrapped(...)

    Example code:

     font.drawWrapped(spriteBatch, "text", x_pos, y_pos, your_area_for_text, BitmapFont.HAlignment.CENTER);
    

提交回复
热议问题