Libgdx Label: multiline text height

I did a little test on LibGdx, on the Multi-Line Label, it seems that I can not get the wrapped line height. Below is the code. Theoretically, the height for aLebel should be> bLabel. But the result looks the same.

the code:

aLabel.setText("this is a super long long long text that need wrapping."); // line wrapped into 3 lines
aLabel.setWrap(true);
aLabel.setWidth(470);
doLog("aLabel.getHeight(): " + aLabel.getHeight());

bLabel.setText("this is short."); // unwrapped line
bLabel.setWrap(true);
bLabel.setWidth(470);
doLog("bLabel.getHeight(): " + bLabel.getHeight());

result:

aLabel.getHeight(): 45.0
bLabel.getHeight(): 45.0

Does anyone have any ideas how to get the actual multi-line growth in LibGdx? Thanks in advance.

+4
source share
4 answers

. , , , .

public Label createLabel() {
    // Create label and set wrap
    Label label = new Label("Some long string here...", skin);
    label.setWrap(true);

    // Pack label
    label.pack(); // This might not be necessary, unless you're changing other attributes such as font scale.

    // Manual sizing
    label.setWidth(textWidth); // Set the width directly
    label.pack(); // Label calculates it height here, but resets width to 0 (bug?)
    label.setWidth(textWidth); // Set width again

    return label;
}

LibGDX: 1.6.4

+3

, , , Label . , , getHeight() , , .

, , BitWappedBound BitmapFont. , :

doLog("aLabel.getHeight(): " + aLabel.getStyle().font.getWrappedBounds(aLabel.getText(), aLabel.getWidth()).height);
+1

, , :

Label label = new Label("Example", new Label.LabelStyle(font, Color.WHITE));
label.setWrap(true);

Table table = new Table();
table.add(label).width(WITH);

For more information on how to use the table, go to: https://github.com/libgdx/libgdx/wiki/Table

0
source

The packet size of the widget corresponds to the size of the pref, nothing more. The width of the width of the label with the wrapper is 0.

Label label = new Label(...);
label.setWrap(true);
label.setWidth(123);
label.setHeight(label.getPrefHeight());
0
source

Source: https://habr.com/ru/post/1545010/


All Articles