JUCE 8 wrap long strings by character in Labels

This seems to have changed between JUCE 7 and JUCE 8 but because of the difficulty of switching between versions, I don’t provide screenshots.

Problem

Let’s say I have a long string without spaces, like a file path, e.g., “/Users/foo/2025-04-19-14-39-36-0e074089-0ffc-41ff-abcf-ecc5ba38d14b/2025-04-19-14-39-36-0e074089-0ffc-41ff-abcf-ecc5ba38d14b.wav”

I want to have a juce::Label of fixed size displaying that string by breaking it into at most 2 lines and using maximum width available on the first line to minimize the risk of displaying ellipsis at the end. I also don’t want to squash the text; for this, I call setMinimumHorizontalScale (1.f); So in this example, I would expect something like

/Users/foo/2025-04-19-14-39-36-0e074089-0ffc-41ff-abcf-ecc5ba38d1
4b/2025-04-19-14-39-36-0e074089-0ffc-41ff-abcf-ecc5ba38d14b.wav

to be displayed, rather than

/Users/foo/
2025-04-19-14-39-36-0e074089-0ffc-41ff-abcf-ecc5ba38d14b/2025-...

How can I achieve this? I have tried using TextEditor but I cannot limit the number of lines to 2 there (we always get scrollable behavior). I am not saying this must be achieved with a juce::Label.

Any help or pointers much appreciated :slight_smile:

If this is rigid (i.e. design does not allow to resize label bounds), surely you can format the string according to your own resize rules, and render only the results in your Label?

Which is to say, the default behavior of Labels’ long string formatting is there mostly for safety, I think - you can of course handle your strings and labels locally without much fuss, no?

Thanks for the reply; indeed, I can format the string myself. However, I haven’t found any examples on how to do it or which classes to use. Can you point me in the right direction? :slight_smile:

I think you’ll need to write your line-wrapping logic yourself. But inserting the char \n works properly for my multi-line labels.

You can break every 120 chars first, and then start to figure out how to break it after a certain pixel length. JUCE8 had some changes there (Deprecation of Font::getStringWidth).

1 Like

Perfect, thanks! Found the answer here: Deprecation of Font::getStringWidth - #7 by reuk