3
\$\begingroup\$

In LibGDX, I want to add some long text content to several Labels. I'm creating those labels in a loop, and the text content is different in each iteration. I want the Label to have a size that fits the text exactly. Is there a way to do this? I did setWrap(true) to the Label.

For reference, consider a Label:

+------------------+
|                  |
+------------------+

One copy of that may be:

+------------------------------------+
| Lorem ipsum dolor sit amet,        |
| consectetur adipiscing elit.       |
| Nec vero alia sunt quaerenda       |
| contra Carneadeam illam sententiam.|
+------------------------------------+

Another one should resize to the content:

+------------------------------------+
| Uterque enim summo bono fruitur,   |
| id est voluptate. Illud non        |
| continuo, ut aeque incontentae.    |
| Occultum facinus esse potuerit,    |
| gaudebit; Multoque hoc melius nos  |
| veriusque quam Stoici. Id Sextilius|
| factum negabat. Ita nemo beato     |
| beatior.                           |
+------------------------------------+

EDIT:

Please note that the horizontal wrapping is working perfectly. I need the Label size to adjust with the text content vertically. Width is fixed. Height is not.

\$\endgroup\$
  • \$\begingroup\$ It should do this automatically (does for me). Can you show us how you create the labels? \$\endgroup\$ – Charanor Apr 25 '17 at 16:09
0
\$\begingroup\$

this may not be exactly what you are looking for, but it might help. I made this function which replace the closest space from the middle of the string by a space line. There is no much to adapt for multi line

It produces this :

+--------------------------+
|Lorem ipsum dolor sit amet|
+--------------------------+

+--------------+
|Lorem ipsum   |
|dolor sit amet|
+--------------+

The code :

public cutAtMiddle()
{
    // Init values needed for the function
    int delta = getText().length; // This need to be high as possible
    int computedDelta; // We declare here so we don't have to do it in the for loop
    int charIndexToReplace = 0; // The char index to replace by the space line

    // Search for the best index to replace
    for (int i = 0; i < label.getText().length; i++)
    {
        // If char is a space
        if (getText().chars[i] == ' ')
        {
            // Compute distance from the middle of the string
            computedDelta = Math.abs(getText().length / 2 - i);

            // If less than current delta : keep in mind index
            if (computedDelta < delta)
            {
                delta = computedDelta;
                index = i;
            }
        }
    }

    // Replace space by space line
    getText().chars[index] = '\n';
}

Then i use it like this :

if (myLabel.getWidth() > maxWidth) 
     myLabel.cutAtMiddle();

It's not such difficult to do the same thing line by line and not specifically by the middle. Hope it helps

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thank you for your time, but may be I wasn't clear enough. The Label is already wrapping horizontally correctly. There's a built-in method setWrap() for that. What I'm expecting is the Label size to adjust with the amount of text content vertically. \$\endgroup\$ – WeirdElfB0y Jan 31 '17 at 9:47
  • \$\begingroup\$ If you add space line, your label's height will increase, is that not what you want ? \$\endgroup\$ – Sebastien Servouze Jan 31 '17 at 9:52
  • \$\begingroup\$ If it resizes, yes! I didn't test this yet. Thank you, I'll check and report. In my experience, labels don't automatically resize during runtime. \$\endgroup\$ – WeirdElfB0y Jan 31 '17 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.