10
votes

I am trying to write a big text using BitmapFont in libGDX . But it shows on one line and the user can see only the first part of the text . How can I make that bitmapFont made new line automatically and show whole text on the screen ?

2
Do you want it to jump to next line automatically when no more space is available?Seyf
yes . shall I use \n ?Vahe Muradyan
Either use \n for manual new lines (font.drawMultiline) or use font.drawWrapped with a wrapwidth to do it automatically.noone
@noone nice to learn of bitmapFont.drawWrapped(), this method is extremely useful.EpicPandaForce

2 Answers

13
votes

Either use \n for manual new lines and render the font via font.drawMultiLine(...).

Or use font.drawWrapped(...) with a wrap width to let libgdx wrap it automatically (manually added \n are still supported).

Update:
With libGDX 1.6 the draw methods were enhanced, and there is no further need for drawMultiLine and drawWrapped, so those were removed. Draw methods can handle multiple lines and a new boolean wrap parameter was introduced.

6
votes

If you take a look at the docs, you'll see that you can use the same font.draw function, except now you're passing the target location width and whether to wrap or not.

draw(Batch batch, java.lang.CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap)