Resize Slider label font when window is resized

I am trying to resize the font of a slider to match the size of the plugin when the plugin is being resized.

I tried overriding getLabelFont in my custom L&F like so:

float fontSize = 50.0f;
Font getLabelFont(Label& label) override
    {
        label.getFont().setHeight(fontSize);
        return label.getFont();
    }
void setFontSize(float size)
    {
        fontSize = size;
    } 

I then call setFontSize(newTextHeight) in PluginEditor’s resized() method.

However, the slider text size is not changing. Any advice?

Maybe just need to call repaint() on the label to force it to redraw?

@HowardAntares unfortunately that did not work

I just needed to set the typeface in my custom L&F instead of using

LookAndFeel::getDefaultLookAndFeel().setDefaultSansSerifTypeface(*text_font);

in PluginEditor.cpp

My default L&F was not the same as my custom L&F.

1 Like

new to JUCE and was having the same problem when I came across this old thread. After playing around with JUCE for a bit, I found an easy way to get the font to get the font to resize in proportion to your window resize so I thought I’d share :slight_smile:

all you have to do is use the function proportionOfWidth(font size you want at default / default width) and your text will stay the same proportion at resize

For example, if you want the font Arial to be set to font size 20 at default at a screen width of 640, you would call your setFont() function and in the parenthesis put (“Arial”, “Regular”, proportionOfWidth(0.03125f)

Hope this helps anyone else struggling to get their font to resize!

@astriiddev can you specify how you achieved this? The setFont only takes one param no?

sure! :slight_smile: sorry I wasn’t clear when I first wrote this

you can either do

auto theFont = juce::Font("Arial", "Regular", proportionOfWidth(0.03125f));
g.setFont(theFont);

or

g.setFont(juce::Font("Arial", "Regular", proportionOfWidth(0.03125f)));

This is a pretty easy, on the fly font change.
However, since writing that comment four months ago, I’ve since changed over to putting it in my default look and feel as based on this tutorial:

as I am using my own ttf font that I created. I wrote it as

const juce::Font OtherLookAndFeel::getCustomFont()
{
    /* Importing binary of custom font */
    static auto typeface = juce::Typeface::createSystemTypefaceFor(BinaryData::amiDOS_ttf, BinaryData::amiDOS_ttfSize);
    return juce::Font(typeface);
}

juce::Typeface::Ptr OtherLookAndFeel::getTypefaceForFont(const juce::Font& f)
{
    /* Returning font typeface */
    return getCustomFont().getTypeface();
}

in my LookAndFeel’s .cpp file.
If you have your custom LnF component set as your default LnF, you can change the height by calling it in the component by writing in paint:

auto theFont = g.getCurrentFont();
theFont.setHeight(proportionOfWidth(0.03125f));
g.setFont(theFont));

Hope this is more help than my last comment! Sorry again for not typing it out well last time :slight_smile:

1 Like

Ah yes! Thank you for taking the time! I got it to work!

1 Like