Lấy kích thước chữ trong libGDX

Trích dẫn từ Stackoverflow

BitmapFont API < 1.5.6

To mesure the width of a String you use your Font and get the bounds of the String, you are going to draw.
BitmapFont.getBounds(String str).width
That's all. You can also get the height to get the right offset for drawing like this. Just replace width with height.
You can use getMultiLineBounds(someString).width for multiple line texts.

BitmapFont API >= 1.5.6

The BitmapFont API changed in 1.5.7 so there is a different way to get the bounds now:
BitmapFont.TextBounds and getBounds are done. Instead, give the string to GlyphLayout and get the bounds using its width and height fields. You can then draw the text by passing the same GlyphLayout to BitmapFont, which means the glyphs don’t have to be laid out twice like they used to. src
Example:
GlyphLayout layout = new GlyphLayout(); //dont do this every frame! Store it as member
layout.setText("meow");
float width = layout.width;// contains the width of the current set text
float height = layout.height; // contains the height of the current set text

Tóm lại: kể từ phiên bản libGDX 1.5.6 trở lên thì khi muốn lấy kích thước của 1 đoạn text ta phải sử dụng GlyphLayout (Có vẻ lằng nhằng hơn :D)
SHARE

Xuho

  • Image
  • Image
  • Image
  • Image
  • Image

0 comments:

Post a Comment