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

橙三吉。 提交于 2019-11-27 05:45:43

问题


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.


回答1:


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




回答2:


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:


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:


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.




回答5:


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;
}


来源:https://stackoverflow.com/questions/7607108/how-do-i-set-a-custom-font-for-the-whole-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!