Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 237
Shivomkara Chaturvedi Avatar asked Sep 30 '11 07:09

Shivomkara Chaturvedi


2 Answers

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

like image 187
Marin Todorov Avatar answered Oct 20 '22 01:10

Marin Todorov


Ican's solution with category might be prefered just to save the day. But avoid using category to override existing methods as apple explains: Avoid Category Method Name Clashes

... If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. ...

Note also that overriding -(id) init; would be safer than overriding -(id)initWithFrame:(CGRect)frame. You would not face with the problem of not receiving touch events when clicking on a label on UIButtons.

like image 35
stanil Avatar answered Oct 20 '22 01:10

stanil