How to get a Label with wrapped text?

15,294

Solution 1

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

Solution 2

I've found that the following code can solve the issue without any table or wrapping container (for libgdx-1.9.6):

label = new Label("Some label", skin);
label.setPosition(600, 50);
label.setWidth(200);
label.setHeight(50);
label.setWrap(true);
stage.addActor(label);

Solution 3

If you just want to specify the preferred size for a single widget, wrap it with a Container.

https://github.com/libgdx/libgdx/wiki/Scene2d.ui#layout-widgets

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Container.html

Something like:

Container<Label> labelContainer = new Container(myLabel);
labelContainer.prefWidth(200.0f);

Keep in mind that the actual size will vary depending on the container hierarchy - for example, the labelContainer above will display differently if placed in another layout object.

The size will also vary depending on the viewport, etc.

Share:
15,294

Related videos on Youtube

talloaktrees
Author by

talloaktrees

Updated on June 03, 2022

Comments

  • talloaktrees
    talloaktrees about 2 years

    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?

  • gvlasov
    gvlasov over 10 years
    Instead of a Table I use this WidgetGroup implementation (see this question for explanation why I need it). Is it only possible to wrap text in a Label that is inside a Table, or could any WidgetGroup implementation provide that functionality?
  • BennX
    BennX over 10 years
    I would give it a try because it is based on the same system. So i think the group should also provide the same features. Regualy you set the width of the container and not of the labele or what ever itself. That should also be the rule for groups
  • salih kallai
    salih kallai over 7 years
    This method is not available in the case of TextButtons