How do I set a custom font for the whole applicati

2019-01-17 18:04发布

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.

5条回答
beautiful°
2楼-- · 2019-01-17 18:21

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.

查看更多
够拽才男人
3楼-- · 2019-01-17 18:25

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.

查看更多
叼着烟拽天下
4楼-- · 2019-01-17 18:26

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

查看更多
戒情不戒烟
5楼-- · 2019-01-17 18:27

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.

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-17 18:37

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;
}
查看更多
登录 后发表回答