iOS中利用 runtime 一键改变字体

一、准备html

咱们新建一个项目名叫ChangeFont,而后我就随便找了个名叫loveway.ttf的字体库拖进去,里面的工程目录大概就是这样的git

571495-91a43c0fac5f91e4.jpg

目录github

如今咱们就简单的直接在storyboard上拖了一个label一个button,约束好,像这样web

571495-d567ca72aadb692b.jpg

storyboardswift

嗯,就这样,很简单,运行app

571495-e81ac83f048a9545.jpg

运行结果ide

好的显示正常,没什么问题,接下来改变字体。函数

二、改变字体字体

咱们以前已经把loveway.ttf这个文件拖进去了,如今在plist文件里面配置一下。打开plist而后加入名为Fonts provided by application的一行,在item里把咱们的字体名字加进去this

571495-f3a34a72dd21beaa.jpg

plist

最后咱们须要保证咱们确确实实是加进来了

571495-89647ae861ac1ba8.jpg

phases

这个时候也许你已经火烧眉毛了,赶忙改字体,以下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//

//  ViewController.m

//  ChangeFont

//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@property (weak, nonatomic) IBOutlet UIButton *myButton;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    _myLabel.font = [UIFont fontWithName:@"loveway.ttf" size:17.0f];

    _myButton.titleLabel.font = [UIFont fontWithName:@"loveway" size:17.0f];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

运行。。。oh no !怎么没变,仍是原来的样子?!

确定是姿式不对,因而百度了一下(虽然我通常都用谷歌),的确这种方法不对。

因而改变思路,先找出字体的名字,Like this,代码改为这样

1

2

3

4

5

6

7

8

9

10

11

- (void)viewDidLoad {

    [super viewDidLoad];

    for(NSString *familyName in [UIFont familyNames]){

        NSLog(@"Font FamilyName = %@",familyName); //*输出字体族科名字

        for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {

            NSLog(@"\t%@",fontName);         //*输出字体族科下字样名字

        }

    }

    _myLabel.font = [UIFont fontWithName:@"loveway.ttf" size:17.0f];

    _myButton.titleLabel.font = [UIFont fontWithName:@"loveway" size:17.0f];

}

运行一看控制台

571495-0aa0ce498e8e115f.jpg

输出的字体名称部分截图

这什么鬼,我哪知道我刚加进去的字体名称是什么,这咋找

因而想出来个办法,再建一个工程,不加入loveway.ttf这个字体,打印出来,一个个对比,多的那个不就是了吗!bingo,因而花了一会功夫终于找出来了,是FZLBJW--GB1-0,无论了,先试试看行不行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

![](http://upload-images.jianshu.io/upload_images/571495-b0d97825e5d33a8a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

- (void)viewDidLoad {

    [super viewDidLoad];

    /*

    for(NSString *familyName in [UIFont familyNames]){

        NSLog(@"Font FamilyName = %@",familyName); //输出字体族科名字

        for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {

            NSLog(@"\t%@",fontName);         //输出字体族科下字样名字

        }

    }

     */

    _myLabel.font = [UIFont fontWithName:@"FZLBJW--GB1-0" size:17.0f];

    _myButton.titleLabel.font = [UIFont fontWithName:@"FZLBJW--GB1-0" size:17.0f];

}

运行,结果以下

571495-c438612edb9e919e.png

改变字体后的运行结果

OK!达到效果了,虽然有点挫,可是效果达到了,还不错。

到这里,基本的改变字体效果已达到。

三、查找字体的一种简单的方法

在上面咱们能够看到,经过对比的方法找到了FZLBJW--GB1-0这个名字,这里,有一种简单的方法,咱们在 Finder 里面找到这个ttf,双击打开(在Xcode里面双击打开没效果),这时候系统就会用苹果自带的字体册打开,以下:

571495-6f429dd17e7f8a8e.jpg

使用字体册打开.rtf

这样咱们就能够看到了这个字体的族科名字,咱们看到的是FZLiBian-S02S,因而咱们在刚才输出所有字体名的控制台搜索一下这个族科名,就能够知道具体的字体名了

571495-71dbd7f8d809bef6.jpg

搜索FZLiBian-S02S

这样就比上面简单多了。

四、进一步的思考

上面例子中简单的说了一下改变字体的方法,虽然成功了,可是咱们不得不思考一下。上面只是两个简单的控件,那么我要是有一堆控件怎么办?或者你能够说我也可用这种方法一个个加,你要是纯代码写的还好,你要是xib写的,难道还要把一个个无用的只是显示一下的label或者button拉出来这样写吗?这样的话,效率确定会很是低,尤为是那些写到一半的大工程,感受这种方法确定是行不通的。

这里利用runtime的class_addMethod、class_replaceMethod、method_exchangeImplementations这几个方法,而后根据+ (void)load这个方法的特性实现(关于+ (void)load这个方法后面会说,或者不懂得童鞋能够先查查资料),代码以下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

//

//  UILabel+FontChange.m

//  LiquoriceDoctorProject

//

//  Created by HenryCheng on 15/12/7.

//  Copyright ? 2015 iMac. All rights reserved.

//

#import "UILabel+FontChange.h"

#import #define CustomFontName @"FZLBJW--GB1-0"

@implementation UILabel (FontChange)

+ (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 {

            if ([UIFont fontNamesForFamilyName:CustomFontName])

                self.font  = [UIFont fontWithName:CustomFontName size:self.font.pointSize];

        }

    }

}

@end

而后不加任何代码以下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

//

//  ViewController.m

//  ChangeFont

//

//  Created by HenryCheng on 16/4/27.

//  Copyright 2016 HenryCheng. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@property (weak, nonatomic) IBOutlet UIButton *myButton;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

//    for(NSString *familyName in [UIFont familyNames]){

//        NSLog(@"Font FamilyName = %@",familyName); //输出字体族科名字

//

//        for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {

//            NSLog(@"\t%@",fontName);         //输出字体族科下字样名字

//        }

//    }

//    _myLabel.font = [UIFont fontWithName:@"FZLBJW--GB1-0" size:17.0f];

//    _myButton.titleLabel.font = [UIFont fontWithName:@"FZLBJW--GB1-0" size:17.0f];

//    _myLabel.tag = 10086;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

运行

571495-c438612edb9e919e (1).png

咱们能够看到字体改变了。

若是有人说我有的想改变字体有的不想改变字体怎么办,我这里有个简单的办法就是设置tag,好比我设置label的tag为10086(随便起的),就让他字体不改变

1462338192307160.png

运行结果

571495-67c22bdfcd9f68b9.png

注意:

一、若是你是代码写控件,你不想改变字体,你只需在建立的时候设置tag为10086

二、上面代码中注释了一行

1

2

3

//if ([self isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {

//      return;

// }

这个是当时写的时候不改变button的title字体设置的,在这里你能够判断那种类型的改哪一种不改,好比说你不想改button的字体,把这一句解注释便可

三、若是你是xib拉的控件,你不想改变字体,你必须在xib界面设置tag为10086,不可加载完毕后在- (void)viewDidLoad里面设置,这仍是由于+ (void)load这个方法

在一个程序(main函数)运行以前,所用到的库被加载到runtime以后,被添加到的runtime系统的各类类和category的+load方法就被调用;(关于这点很容易经过打印语句来验证);

若是父类和子类的+load方法都被调用,父类的调用必定在子类以前,这是系统自动完成的,子类+load中不必显式调用[super load];;

这里只是简单的说一下,具体不理解的能够翻翻官方文档

五、最后

关于代码的解释,在工程里都已经注释的很是清楚了,这里就很少说了,不清楚的童鞋能够给我留言。具体用法很简单,你只须要将UILabel+FontChange.h和UILabel+FontChange.m拉进你的工程便可。

须要下载更多字体的能够在字体库下载,全部的代码均可以在这里下载。

最近在看swift,作了一下笔记,后面会为你们分享总结的一些swift tips。

最后,若是你有什么建议或者指正的地方请给我留言,若是喜欢或者对你有帮助的话,就请star一下吧,谢谢!

相关文章
相关标签/搜索