iOS 全局修改字体

来源:互联网 发布:为知笔记插件 编辑:程序博客网 时间:2024/06/07 05:02
全局修改Lable/Button字体

  本次版本需求要把原来的字体全改掉,由于项目中有的是代码创建的,有的是XIB中直接改的,一个一个改工作量太大,使用运行时可以很轻松的实现
 
     首先,项目中大多数设置字体的控件有 Lable, Button等,控件的初始化,有三种方式,init,initWithFrame,awakeFromNib
     所以,使用运行时替换这三个方法就可以了。
 
     为方便在多个类中使用运行时交换方法,可以创建一个头文件 SwizzlingExchange.h ,实现我们需要的交换方法
 SwizzlingExchange:
复制代码
#ifndef SwizzlingExchange_h#define SwizzlingExchange_h#import <objc/runtime.h>static inline void swizzling_exchangeMethod(Class clazz, SEL originalSelector, SEL swizzledSelector) {    Method originalMethod = class_getInstanceMethod(clazz, originalSelector);    Method swizzledMethod = class_getInstanceMethod(clazz, swizzledSelector);    BOOL success = class_addMethod(clazz, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));    if (success) {        class_replaceMethod(clazz, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));    } else {        method_exchangeImplementations(originalMethod, swizzledMethod);    }}#endif /* SwizzlingExchange_h */
复制代码

 

 
     然后创建我们 Lable、Button等控件的分类,在 +load方法里面 交换我们的三个函数,这样我们首次创建的控件字体就是我们的默认字体,对于某些特殊的地方使用的不是默认字体,重新设置字体就可以了。
 Lable
复制代码
////  UILabel+ChangeFont.m//  DFRomwe////  Created by 王卫亮 on 16/7/13.//  Copyright © 2016年 heyan. All rights reserved.//#import "UILabel+ChangeFont.h"#import "SwizzlingExchange.h"#define kLableFont @"Snell Roundhand"  // 测试字体,容易看出来有没有全改掉@implementation UILabel (ChangeFont)+(void)load {    //只执行一次这个方法    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        swizzling_exchangeMethod([self class], @selector(init), @selector(myInit));        swizzling_exchangeMethod([self class], @selector(initWithFrame:), @selector(myInitWithFrame:));        swizzling_exchangeMethod([self class], @selector(awakeFromNib), @selector(myAwakeFromNib));    });}- (instancetype)myInit {    id __self = [self myInit];    UIFont * font = [UIFont fontWithName:DefaultFont size: self.font.pointSize];    if (font) {        self.font = font;    }    return __self;}- (instancetype)myInitWithFrame:(CGRect)rect {    id __self = [self myInitWithFrame: rect];    UIFont * font = [UIFont fontWithName:DefaultFont size: self.font.pointSize];    if (font) {        self.font = font;    }    return __self;}- (void)myAwakeFromNib {    [self myAwakeFromNib];    UIFont * font = [UIFont fontWithName:DefaultFont size: self.font.pointSize];    if (font) {        self.font = font;    }}@end
复制代码

Button:

复制代码
////  UIButton+ChangeFont.m//  DFRomwe////  Created by 王卫亮 on 16/7/13.//  Copyright © 2016年 heyan. All rights reserved.//#import "UIButton+ChangeFont.h"#import "SwizzlingExchange.h"#define kLableFont @"Snell Roundhand"  // 测试字体,容易看出来有没有全改掉@implementation UIButton (ChangeFont)+(void)load {    //只执行一次这个方法    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        swizzling_exchangeMethod([self class], @selector(init), @selector(myInit));        swizzling_exchangeMethod([self class], @selector(initWithFrame:), @selector(myInitWithFrame:));        swizzling_exchangeMethod([self class], @selector(awakeFromNib), @selector(myAwakeFromNib));    });}- (instancetype)myInit {        id __self = [self myInit];    UIFont * font = [UIFont fontWithName:DefaultFont size: self.titleLabel.font.pointSize];    if (font) {        self.titleLabel.font = font;    }    return __self;}- (instancetype)myInitWithFrame:(CGRect)rect {    id __self = [self myInitWithFrame:rect];    UIFont * font = [UIFont fontWithName:DefaultFont size: self.titleLabel.font.pointSize];    if (font) {        self.titleLabel.font = font;    }    return __self;}- (void)myAwakeFromNib {    [self myAwakeFromNib];    UIFont * font = [UIFont fontWithName:DefaultFont size: self.titleLabel.font.pointSize];    if (font) {        self.titleLabel.font = font;    }}@end

0 0
原创粉丝点击