TextButton Font size without LaF

Hi all,
I need to have 6 TextButtons that all look the same but with different font sizes.
Do I really need to subclass from my CustomTextButtonLookAndFeel 6 times and change the font size there? this seems absurd.


LookAndFeel_V2::drawButtonText uses drawFittedText, that automatically adapts the font size so the whole text is visible.
If you don’t want that, you can override that method. But you are right, it would be overly complicated to use several lookAndFeel classes.
Instead you could put a property into the button, that you use in your drawButtonText method:

button.getProperties()["fontsize"] = 20;
auto fontsize = button.getProperties().getWithDefault ("fontsize", 12);
g.setFont (fontsize);
g.drawText (...);  // don't use drawFittedText to force your size
1 Like

Hi Daniel, thanks for your help!

This results in a (no viable overloaded “=”) error.

Oh, I am sorry, since it returned a reference, I thought it would accept it as lvalue…
But you can use the set of the NamedValueSet instead:

button.getProperties().set ("fontsize", 20);

Hope that’s better :wink:


Amazing idea :slight_smile:

This was a really helpful suggestion, thanks @daniel !

Like @rollo, I was looking at needing multiple font sizes for the same type of Component, and scratching my head about to how to avoid creating multiple LookAndFeel classes.

Rather than create a fontsize property, however, I created a guiClass property, with the idea of using named “classes” for styling, in the CSS sense of the word.

So the guiClass property might hold a value of “sidebar” for one TextButton instance, “header” for another instance, and “main” for a 3rd instance.

Then in the LookAndFeel method getTextButtonFont, I can fetch the guiClass property, and test its value. Depending on which guiClass it is, the actual font sizes can then be set.

This approach keeps the font size numbers within the LookAndFeel class, rather than spreading those numbers around different spots in the codebase.

That is a very nice approach, I like that.

I wish there was a setting that existing LookAndFeels would react to (and not just as default as in the static getDefaultFont()). The current approach makes it impossible to have different fonts in the same type of component with the existing LookAndFeels.

This would help in my plugin editor engine PluginGuiMagic as well. I don’t want to limit the user to use only my bespoke LookAndFeels. And it will be hard to communicate, why there is a font size and font family setting, that does nothing in the stock LookAndFeels.
My only option would be to patch up the existing LookAndFeels.