Setting Default Font


#1

I’ve checked the forum for this, but it seems none of the current solutions are working for me.

I want to set a default font for all components to use, so in my custom LookAndFeel class’ constructor I wrote setDefaultSansSerifTypefaceName("Avenir Next"); but this does not change the font.

The other functions in my LookAndFeel are working as expected. Also, the font exists on my system and works when I set the font for individual components.
Thanks!


#2

When I add this line to the projucer’s initialise method:

    LookAndFeel::getDefaultLookAndFeel().setDefaultSansSerifTypefaceName ("Avenir Next");

it works.

So you must be doing something silly. e.g. probably modifying a different look+feel object to the one you’re using.


#3

Thanks Jules!
This does indeed work for me as well.

Although, I would’ve expected the setDefualtSansSerifTypefaceName call to work in the constructor of my custom LookAndFeel. The rest of my custom LookAndFeel works as expected within my application except for this call. For example here’s a summary of what my code looks like:

CustomLookAndFeel.h

class CustomLookAndFeel : public LookAndFeel_V3
{
public:
    CustomLookAndFeel()
    {
        setDefaultSansSerifTypefaceName ("Avenir Next"); // This call doesn't work
    }
    
    //  The rest of my LookAndFeel which does work . . .
};

MainComponent.cpp

class MainContentComponent   :  public Component
{
public:
    MainContentComponent()
    {
        setLookAndFeel (&customLookAndFeel);
    }

    // The rest of the class . . . 

private:
    CustomLookAndFeel customLookAndFeel;
};

I’m probably just fundamentally misunderstanding something about this.

Thanks again for your solution though! I will definitely be using that solution. I just wanted to clarify my understanding about what I was trying to do earlier with the LookAndFeel Constructor.


#4

Only the global L+F is used for this.

(How else could the Font objects all over the codebase know that you were expecting them to use this customLookAndFeel instance that you created?)


#5

Okay I see. Thanks for your help Jules!


#6

Well, you could expect that all the Component’s that have your custom LookAndFeel class would use the Font right? I mean it’s a member method of the LookAndFeel class and it’s not a static one so the assumption is not so un-logical is it?


#7

Well no - think about the code you actually write. E.g. if you create a font object

Font myFont (15.0f);

… then like I said above, how could it possibly know which component to get the L+F from? What if you have two different components with two L+Fs and use this same font object in both of them?


#8

True that makes sense, but i think the OP was expecting that his JUCE Buttons or Labels would start using the Font he specified in his Custom LookAndFeel.
I know you can override all the draw methods and do anything you like but i can understand why he is confused. You told him he was probably doing something ‘silly’…


#9

Sure, I understand why you’d expect it to happen.

Well, yes, but to be fair if I’d seen the same symptoms in my own code, my first reaction would also have been to assume I’d made a silly mistake. Everyone makes silly mistakes, it’s not a criticism!