利用iOS的runtime改变字体

作者: suncheng | 来源:发表于2016-05-26 17:49 被阅读339次

在项目开发中有可能遇到一种情况是,项目已经开发到一半或者已经完成了,这时候产品经理说 “来,把APP上的所有字体改成‘xxx’字体”!以下有三种方法可以达成此需求。

继承

可以写一个继承自UIFont的类,在里面自定义一个设置字体的方法,在用到设置字体的时候都调用这个方法就可以了。但这样做有一个明显的缺点:项目里有很地方都要用到设置字体,就需要把所有相应设置字体的方法改成你自定义的方法,工作量巨大,容易出差错。

category

其实要达成这个目的还可以为UIFont建立一个category,重写systemFontOfSize:,覆盖它的原有方法实现,但是这样做就无法调用系统原有的方法,而且苹果也不建议在category中重写方法:

category重写systemFontOfSize.png

会出现上图中警告

所以我们的解决方法就是利用iOS的runtime另写一个方法来和systemFontOfSize:交换。

利用runtime另写一个方法来和systemFontOfSize:交换

  • Method Swizzling

主要通过以下面的方法来交换
method_exchangeImplementations(originalMethod, swizzledMethod);

+  (void)load{
 //只执行一次这个方法
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSelector = @selector(systemFontOfSize:);
        SEL swizzledSelector = @selector(mysystemFontOfSize:);
        // Class class = class_getInstanceMethod((id)self);
        Method originalMethod = class_getClassMethod(class, originalSelector);
        Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
        BOOL didAddMethod = class_addMethod(class,originalSelector,
                                            method_getImplementation(swizzledMethod),
                                            method_getTypeEncoding(swizzledMethod));
        if (didAddMethod)
        {
            class_replaceMethod(class,swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
            
        }
        else
        {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

  • IMP指针

IMP是“Implementation”的缩写,它指向一个方法的实现,每个方法都对应一个IMP,我们可以直接调用指针来达到目的。

#import "UIFont+changeFont.h"
#import <objc/runtime.h>
#define CustomFontName @"Menlo-Regular"
typedef id (*_IMP)(id,SEL,...);  // 有返回值
typedef id (*_VIMP)(id,SEL,...); // 无返回值
@implementation UIFont (changeFont)
+(void)load{
static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{ 
         Method systemFontOfSize = class_getClassMethod(self, @selector(systemFontOfSize:));
         _IMP systemFontOfSize_IMP = (_IMP)method_getImplementation(systemFontOfSize);
        method_setImplementation(systemFontOfSize, imp_implementationWithBlock(^(id target,SEL action,CGFloat fontSize){ 
            systemFontOfSize_IMP(target,@selector(systemFontOfSize:));
            UIFont *font = [UIFont fontWithName:CustomFontName size:fontSize];
            return font;
        }));
});
@end

直接调用IMP指针是一种高效简单的方法,以后有类似的问题不妨一试!

相关文章

网友评论

  • 空转风:楼主你好,这样的话只是在打开app的时候改变字体,但是如果是这样一个需求“打开app的时候显示系统字体,点击某个按钮才改变字体”这样要如何用runtime实现?求教
    德坤柳:@年光逝也被僵尸号占了 嗯嗯,有道理,反正我字号设置都有过systemFontSize和boldSystemFontSize,只要重写它们就可以了,还好没有无脑设变量到处改,谢谢层主的回答!
    空转风:@德坤哥 重写那个systemFontSize方法,如果是你规定的字符串就改变字体,然后点击按钮的时候遍历一下就好,如果是多个页面就发通知吧。runtime的话就是交换系统方法,也是类似思路
    德坤柳:我们现在也是需要这个需求,请问你实现了吗
  • 蛋Dan:systemFontOfSize_IMP(target,@selector(systemFontOfSize:)); 麻烦问下这行代码什么意思,好像注释掉也不会出错。
    suncheng:@感受阳光 这句代码是“调用原有方法实现”,这样做以后就可以调用系统原有的方法。
  • ldldlkdldld:引发过bug吗?这种实现方式?
    suncheng:@周永强 不会啊

本文标题:利用iOS的runtime改变字体

本文链接:https://www.haomeiwen.com/subject/zulxdttx.html