Fit Size of Label to Size of Text?

Hi!

I have a text Label. On a Label you can set the text with

Label.setText("Hello World")

and you can set the boundaries with

Label.setBounds(x,y,width,height).

Is there a way to determine, how much space the text of the label is going to take? I want to fit the bounds of the label to the size of the text. I.e. I would like to do something like:

Label.setBounds(x,y, LabelText.width, LabelText.height).

You could create a TextLayout with same font and text then call getWidth() on it.

https://www.juce.com/api/classFont.html#aed4d0579fb11ca1bba6c68e5bd02e1b2

Font::getStringWidth() and Font::getHeight()

Rail

Doh - forgot about that!

Thanks!

Since this was so many clicks and the link is dead… here’s the new link:

https://juce.com/doc/classFont#aadc076ad44f50a63dc7d8c5a3ca54899

Rail

1 Like

This is also dead

https://docs.juce.com/master/classFont.html#a23539f1990fd39341b84566a413c051d

If the string contains characters not in the font, the width will be wrong. But drawText will render the string correctly, as it uses the font fallback mechanism, whereas getStringWidth does not…