Can JUCE tell me the precise amount of space my text needs (without squashing the text)

I think it is great that JUCE has functions for squashing text into a particular rectangle, but personally I think that this sometimes looks tacky. Sometimes I would prefer to just resize my buttons and components programmmatically, so that the text is not squashed. Or alternatively, if the text was much smaller than anticipated, then I would like to make the component smaller, to avoid wasting screen space. But the problem is, I have no idea how to programmatically determine the screen area that the text would comfortably fit into without JUCE squashing the text. Is there a function for this, or do I need to build this manually?

1 Like

You can use Font::getStringWidth() or GlyphArrangement::getBoundingBox().


You can also disable squashing globally by calling


If the text doesn’t fit you’ll get “…” or text cropping instead of squashing.


Just thought I would mention that there is an issue with JUCE here. I had a reason to use a static Font and then used this static object to measure the width of text (Font::getStringWidth). But in making this Font object static, it caused my app to have some unusual leaks. These were leaks of core JUCE classes, not anything referenced in my own code, or even classes that I have ever heard of. This made it quite amusing for me to read Jule’s comments code-shaming me for supposedly causing the leaks. Very presumptious!

The Font class has a singleton inside it for managing a cache of typefaces. By making it static you are probably extending its life past the point where DeletedAtShutdown::deleteAll() cleans up all the app’s singletons (usually in shutdownJuce_GUI or when JUCEApplicationBase is being cleaned up).

Ok. I’ve stopped using it as static. Just wanted to let you know, just in case this was not a deliberate feature.