iOS动态改变APP字体

改变整个APP的所有控件的字体分为两种情况:
1.在程序启动的时候的改变字体,并且在程序运行过程中不需要再改变。
2.在程序运行的过程中动态改变APP的字体(比如微信等)。

一、只是改变一次

有两种方法:
1.使用runtime改变,不能对控件进行单独设置。(可以设置控件的tag值进行判断处理)
2.重写 控件的 initWithFrame方法。如果对控件进行单独处理的方法与重写的方法重复,会使用单独处理的方法。
使用runtime
创建UIlabel的子类

#import "UILabel+changeFont.h"
#import <objc/runtime.h>

@implementation UILabel (changeFont)

+ (void)load {
    //方法交换应该被保证,在程序中只会执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //获得viewController的生命周期方法的selector
        SEL systemSel = @selector(willMoveToSuperview:);
        //自己实现的将要被交换的方法的selector
        SEL swizzSel = @selector(myWillMoveToSuperview:);
        //两个方法的Method
        Method systemMethod = class_getInstanceMethod([self class], systemSel);
        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
        
        //首先动态添加方法,实现是被交换的方法,返回值表示添加成功还是失败
        BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            //如果成功,说明类中不存在这个方法的实现
            //将被交换方法的实现替换到这个并不存在的实现
            class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
        } else {
            //否则,交换两个方法的实现
            method_exchangeImplementations(systemMethod, swizzMethod);
        }
        
    });
}

- (void)myWillMoveToSuperview:(UIView *)newSuperview {
    
    [self myWillMoveToSuperview:newSuperview];
    if ([self isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
        return;
    }
    if (self) {
        if (self.tag == 10086) {
            self.font = [UIFont systemFontOfSize:self.font.pointSize];
        } else {
            self.font = [UIFont systemFontOfSize:30];
            self.backgroundColor = [UIColor redColor];
        }
    }
}

@end

重写 控件的 initWithFrame方法
创建UIlabel的子类

#import "UILabel+changeFont.h"

@implementation UILabel (changeFont)

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
       [self setGlobalFont];
    }
    return self;
}
// 如果是xib或storyboard创建会走该方法
- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setGlobalFont];
}

- (void)setGlobalFont
{
    // 不改变 textfield
    if ([self isKindOfClass:NSClassFromString(@"UITextFieldLabel")]) {
        return;
    }
    // 不改变 button
    if ([self isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
        return;
    }
    
    self.font = [UIFont systemFontOfSize:30];
    self.backgroundColor = [UIColor redColor];
    
}

@end

二、动态改变APP所有字体

重写控件的 initWithFrame方法,添加一个通知,在需要改变的时候 post这个通知就可以了。
创建UIlabel的子类

#import "UILabel+changeFont.h"

@implementation UILabel (changeFont)

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setGlobalFont) name:@"font" object:nil];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setGlobalFont) name:@"font" object:nil];
}

- (void)setGlobalFont
{
    if ([self isKindOfClass:NSClassFromString(@"UITextFieldLabel")]) {
        return;
    }
    if ([self isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
        return;
    }
    self.font = [UIFont systemFontOfSize:30];
    self.backgroundColor = [UIColor redColor];
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容