Multiple fonts per component


#1

Hey all - is it possible to set different font sizes (or ideally fonts) in the same component?

I’m trying to do something like this…

    Font font ("Helvetica Neue", 20 , Font::plain);
    g.setFont (font);
    g.drawText("size 20 text", 0,0, width, height, Justification::left);

    font.setHeight(10);
    g.drawText("size 10 text", 0,0, width, height, Justification::left);

If I run the debugger, the height of the font does get set to 10, but it draws the 2nd line of text as size 20.

If I make an entirely new font instead, it doesn’t draw the text at all.

Is the answer to make a component for each style of font I want to use - kind of like a class in CSS?

Many thanks!


#2

I think you may be missing a second g.setFont (font); line.

Eg:

Font font ("Helvetica Neue", 20 , Font::plain);
g.setFont (font);
g.drawText("size 20 text", 0,0, width, height, Justification::left);

g.setFont (font.withHeight(10));
g.drawText("size 10 text", 0,0, width, height, Justification::left);

The g.setFont() method doesn’t save a reference to your Font object, so any changes to the local font variable won’t update the graphics context.


#3

Ahhh… thank you!


#4

He can just do g.setFont(10) to change the height of the current font. Don’t be afraid to look at the implementations of the Juce classes for inspiration and guidance on usage.


#5

Thank you that’s perfect.


#6

Worth a look at AttributedString as well depending on what you are trying to do