LookAndFeel_V4 difficulties applying Typeface to just one component

I am having trouble following the LookAndFeel Tutorial.

According to the “Cusomizing Color” Section, I coded this:

in My Component.h:

private:
    juce::LookAndFeel_V4            m_LookAndFeelIcons;

and in it’s constructor:

m_LookAndFeelIcons.setDefaultSansSerifTypeface(MyBinaryAssets::MyTypeFace);
someBTN.setLookAndFeel(&m_LookAndFeelIcons);

But the Button does not change it’s font…?

When I do something like:

someBTN.getLookAndFeel().setDefaultSansSerifTypeface(MyBinaryAssets::MyTypeFace);

It does work, but it changes the Font of all my buttons…
Am I missunderstanding something?

1 Like

Sounds like the button is sharing the same look&feel as all your other buttons, not using the one you’ve declared. Did you set it as the look&feel for that component (using setLookAndFeel(m_LookAndFeelIcons))?

I notice a quite similar thing here!

I called

someBTN.setLookAndFeel(&m_LookAndFeelIcons);

as mentioned earlier… Or what are you referring to?

Overwriting the getTextbuttonFont() in the LookAndFeel_V4 might work for me, by manually setting the font there…

(At the moment , I get some scaling issues there and it seems to be too complicated, but I think it is a possible solution…)

Sorry, I missed that line. You might try debugging and see if it’s actually using the Look&Feel that you are expecting.

Also, when you use getLookAndFeel() instead of using that Look&Feel directly, are you doing so after having set it for the button, like this:?

someBTN.setLookAndFeel(&m_LookAndFeelIcons);
someBTN.getLookAndFeel().setDefaultSansSerifTypeface(MyBinaryAssets::MyTypeFace);

If not, then getLookAndFeel will return the default Look&Feel, which is apparently shared by the other buttons. That would explain setting all the buttons’ fonts.

But for the issue at hand, which is the button NOT using the expected typeface, perhaps the Font is the problem somehow? Not sure how the button gets its Font, but the function setDefaultSansSerifTypeface() sets the member defaultTypeface, but that member only gets returned from getTypefaceForFont() if the typeface name matches the default sans serif font name. Perhaps the issue lies there?

Debugging seems to be problematic right know. I don’t know what the issue is, but I can not “step into” the setLookAndFeel function, with the debugger(I use Visual Studio). But, when I hover over the m_LookAndFeelIcons, I find my Typeface set as default there in the debugger.

When I use getLookAndFeel(), the way you mentioned, the Font of everything changes…

You should at least be able to assign the return value of getLookAndFeel() to a local pointer, and compare that pointer value with the address of m_LookAndFeelIcons. That would tell you if they are the same. And you could find where the function getTypefaceForFont() is being called, and see if that is doing what you expect.

Assigning to local pointers shows me, that getLookAndFeel() and m_LookAndFeelIcons have the same address.

Now my Debugger is working as expected and I can see, that this is working fine too:
void LookAndFeel::setDefaultSansSerifTypeface (Typeface::Ptr newDefaultTypeface)
it does set the new Typeface!

When I call:
getLookAndFeel().getTypefaceForFont(juce::Font());
It does give me my expected TypeFace.

By the way, when I set a color in m_LookAndFeelIcons (like in the tutorial), it is applied to the Button. It is just the Typeface that is not working.

I tried a different font from the web and the same behaviour appears. I think it is not related to my font… And as earlier mentioned, my font can be displayed, when I set it as default for the overall LookAndFeel.

I would expect everything to be working, when I do it this way:

class CustomLookAndFeel : public juce::LookAndFeel_V4
{
    public:
    CustomLookAndFeel() {
        this->setDefaultSansSerifTypeface(MyAssets::MyTypeFace);
    }
}

In that case, it probably uses the V2 getTextButtonFont(), but it does not use my font here:

Font LookAndFeel_V2::getTextButtonFont (TextButton&, int buttonHeight)
    {
        return Font (jmin (15.0f, (float) buttonHeight * 0.6f));
    }

Maybe there is a JUCE problem? Because, when I adjust/overwrite the function like:

juce::Font getTextButtonFont(juce::TextButton& t, int buttonHeight) override { 
    return juce::Font(getTypefaceForFont(juce::Font())).withHeight(juce::jmin(15.0f, (float)buttonHeight * 0.6f));
}

It works as expected… But this can not be the solution, right? I have different Components with text, which will be using my CustomLookAndFeel and I don’t want to always overwrite this function for all the component types.

Well, the function getTextButtonFont() does get passed a reference to the specific TextButton. If you check the button’s name, then you can apply that fix for just that button, and call the default code for others. That’s what our LookAndFeel classes do in many cases.

I actually don’t want to deal with getTextButtonFont()… when not necessary!

I would expect this to do the job:

I just want to assign a custom LookAndFeel to some components to set my TypeFace as default font for them (Actually as simple as that). All of the components, using the LookAndFeel shell behave the same, using my font, I don’t want to filter out button names for differnent behaviour. Should the code, I quoted, to the job or not?

At the moment, I also need to override the getFont functions, otherwise my Buttons do not get the desired font:…

Furthermore I had to override these the same way, to apply the font to other component types:

  • getComboBoxFont()
  • getLabelFont()
  • getTabButtonFont()
  • getSidePanelTitleFont()

Anyway, thank you for your help so far @HowardAntares ! Maybe I am just missunderstanding something…

1 Like