9

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.

Rais Alam
  • 6,970
  • 12
  • 53
  • 84
Shivomkara Chaturvedi
  • 1,697
  • 7
  • 28
  • 42

5 Answers5

5

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

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73
  • 11
    This is a world of pain waiting to happen. You are replacing the standard implementations of `awakeFromNib` and `initWithFrame`, now what if Apple adds some important bit of code there? – zoul Sep 30 '11 at 07:39
  • 4
    I also got an idea. Because your method is too long. I just created a category like this. in my app delegate @implementation UILabel (labelFont) - (void)awakeFromNib { [super awakeFromNib]; self.font = [UIFont fontWithName:@"Bauhaus Md BT" size:self.font.pointSize]; } – Shivomkara Chaturvedi Sep 30 '11 at 07:39
  • @zoul is right - the best best solution will be to have a custom class, which inherits from UILabel and has a custom default font. And then use this custom class for all UILabels in IB – Marin Todorov Sep 30 '11 at 07:42
  • Is there any issue with my method which I used? If then please tell me I want to reduce my rework – Shivomkara Chaturvedi Sep 30 '11 at 07:51
  • 1
    @Shivam: Yes. You are overriding the standard methods of `UILabel` class using a category. The problem is that there’s no sane way to call the code you have replaced – calling `super` in a category does *not* call the code you have overriden. Right now the standard implementation of `[UILabel awakeFromNib]` does not seem to do anything important, so that the “solution” works for now. This might change in any minor update and your application will break. – zoul Sep 30 '11 at 08:20
  • Yes you are right, But my problem is that my application is ready and now client want to change the font of application. So if I use your method then I need to change my all UILabel with new one. am I right? Hey! There is an another issue. Issue is that If I create any uilabel,button from code then its not changing foonts – Shivomkara Chaturvedi Sep 30 '11 at 12:36
  • Shivam I believe you should explain your client what the situation is and bill him for your overtime if you are going to spend more time because he came up with this requirement when the project was already to be finished. that said: awakeFromNib is the method that takes care of the customization of InterfaceBuilder laid labels, initWithFrame is the one doing the customization for programmatically created labels – Marin Todorov Sep 30 '11 at 13:46
  • Apart from the issue with categories replacing methods (the super calls in a category do not call the original method), I see another huge problem with the category approach - you are setting default fonts for labels but ANY UILabel that has a different size set in IB will be using whatever font is specified in IB, or be set to this 12 point size instead of whatever you asked for in IB. – Kendall Helmstetter Gelner Feb 19 '13 at 21:26
  • Your code works perfect and I don't have to customize every uielement in all my classes. I used it also for my UIButtons, but right now I can't access the buttons when clicking on a label on the button. The buttons works just arround the label. Maybe you have an advice how to solve this problem. – Alex Cio May 21 '13 at 14:08
  • 1
    People, DON'T USE THIS! First an economic reason: your work today is valued less than tomorrow, so saving time today that will make you spend more time tomorrow is not a wise decision. And then the direct reason: this can change anytime! Please learn about method swizzling to do it in a safer way. – Fábio Oliveira Apr 10 '14 at 14:01
3

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.

stanil
  • 31
  • 1
1

Is this what you mean?

@interface GlobalMethods
+(UIFont *)appFont;
@end

@implementation GlobalMethods
+(UIFont *)appFont{
    return [UIFont fontWithName:@"someFontName" size:someFontSize];
}
@end

...
[self.titleLabel setFont:[GlobalMethods appFont]];

In case you want to do it somehow automatically (without using setFont on each control), I don't believe it's possible.

alex-i
  • 5,406
  • 2
  • 36
  • 56
0

CustomLabel.h

#import <UIKit/UIKit.h>

@interface VVLabel : UILabel

@end

CustomLabel.m

#import "CustomLabel.h"
#define FontDefaultName @"YourFontName"
@implementation VVLabel
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder: aDecoder];
    if (self) {
        // Initialization code
        // Static font size
        self.font = [UIFont fontWithName:FontDefaultName size:17];


        // If you want dynamic font size (Get font size from storyboard / From XIB then put below line)
        self.font = [UIFont fontWithName:FontDefaultName size:self.font.pointSize];

    }
    return self;
}
Vivek
  • 4,916
  • 35
  • 40
0

If you can limit your application – or this particular feature – to iOS 5, there’s a new API coming that lets you skin the default UI very conveniently. I can’t give you details, since they are still under NDA at the time I am writing this. Take a look at iOS 5 beta SDK to find out more.

zoul
  • 102,279
  • 44
  • 260
  • 354