Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Label with wrapped text?

Tags:

java

label

libgdx

I ave tried the following code:

Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label);
...

and yet all I get is a very wide line that draws off the screen. How to get a Label with wrapped text?

like image 746
talloaktrees Avatar asked Sep 01 '13 10:09

talloaktrees


People also ask

How do I get text to wrap around?

On the Format tab, click the Wrap Text command in the Arrange group, then select the desired text wrapping option.


1 Answers

This is the same issue as seen in slider always has default width or "Slider always has default width".

You need to put that label into a table and add the right size to the cell of the table where the label is.

UI widgets do not set their own size and position. Instead, the parent widget sets the size and position of each child. Widgets provide a minimum, preferred, and maximum size that the parent can use as hints. Some parent widgets, such as Table, can be given constraints on how to size and position the children. To give a widget a specific size in a layout, the widget's minimum, preferred, and maximum size are left alone and size constraints are set in the parent.

Source: From the libgdx wiki Scene2D

The solution:

Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label).width(10f);// <--- here you define the width
like image 121
BennX Avatar answered Oct 26 '22 04:10

BennX