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

前端 未结 5 480
猫巷女王i
猫巷女王i 2020-12-09 06:02

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 l

相关标签:
5条回答
  • 2020-12-09 06:37

    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.

    0 讨论(0)
  • 2020-12-09 06:38

    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;
    }
    
    0 讨论(0)
  • 2020-12-09 06:47

    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.

    0 讨论(0)
  • 2020-12-09 07:00

    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.

    0 讨论(0)
  • 2020-12-09 07:01

    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

    0 讨论(0)
提交回复
热议问题