Search code examples
objective-cfontsuifontsystem-font

only change font size (and not font name)


What I want to do is change the font size.

I know below statement will change the font size, but it changes the font name because we are using systemFontOfSize

[UIFont systemFontOfSize: 13.0];

I know alternate option is as mentioned below.

[myLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]];

but I don't want to use fontWithName as I am setting that in IB.

I don't want to play with font name as my app is multi-language and hence I don't want to play with font name.

Any idea how can I just change fontsize and don't change the fontname.


Solution

  • Too bad that the font metrics properties of UIFont are readonly. It'd be nice if they could be adjusted dynamically without having to do this below:

    UIFont * fontFromLabel = myLabel.font;
    
    // now we have the font from the label, let's make a new font
    // with the same font name but a different size
    if(fontFromLabel)
    {
        // 13, or whatever size you want
        UIFont * newFontForLabel = [UIFont fontWithName: fontFromLabel.fontName size: 13.0f]; 
        if(newFontForLabel)
        {
            [myLabel setFont: newFontForLabel];
        }
    }