How do I set a custom font for the whole application?

Shivomkara Chaturvedi picture Shivomkara Chaturvedi · Sep 30, 2011 · Viewed 12.1k times · Source

Is there any way to How to Apply global font [new custom font] to whole application in iphone objective-c.

I know that we can use below method to set font for each label

[self.titleLabel setFont:[UIFont fontWithName:@"FONOT_NAME" size:FONT_SIZE]];

But I want to change for whole application. Please help me if anyone know.

Answer

Marin Todorov picture Marin Todorov · Sep 30, 2011

Apparently to change ALL UILabels altogether you will need to setup a category on UILabel and change the default font. So here's a solution for you:

Create a file CustomFontLabel.h

@interface UILabel(changeFont)
- (void)awakeFromNib;
-(id)initWithFrame:(CGRect)frame;
@end

Create a file CustomFontLabel.m

@implementation UILabel(changeFont)
- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setFont:[UIFont fontWithName:@"Zapfino" size:12.0]];
}

-(id)initWithFrame:(CGRect)frame
{
    id result = [super initWithFrame:frame];
    if (result) {
        [self setFont:[UIFont fontWithName:@"Zapfino" size:12.0]];
    }
    return result;
}
@end

Now ... in any view controller you want these custom font labels, just include at the top:

#import "CustomFontLabel.h"

That's all - good luck