美文网首页
替换字体(正则替换->runtime替换)

替换字体(正则替换->runtime替换)

作者: 小木工_aedc | 来源:发表于2021-03-10 17:29 被阅读0次

项目需求,需要将APP内的所有字体,替换为客户提供的字体。

1、字体添加到项目中
使用的是xcode12.2,把字体添加到info.plist中后,打印字体,始终不起作用。
后面发现左边文件列表的info.plist和target中info下的plist内容竟然不一致,在此修改后,字体顺利加载到APP中。

2、整理APP内所有字体为统一格式

先定好通用字体宏定义

#define FontRegular(fontSize)    [UIFont fontWithName:@"PingFangSC-Regular" size:fontSize]     // 常规体
#define FontMedium(fontSize)   [UIFont fontWithName:@"PingFangSC-Medium" size:fontSize]    // 中等
#define FontBold(fontSize)         [UIFont fontWithName:@"PingFangSC-Semibold" size:fontSize]  // 粗体

然后使用正则替换为通用字体
发现项目中使用字体的格式太多,下面仅列出我遇到的几种:

\[UIFont fontWithName:@"()" size:(\d*)]  替换为  FontRegular($2)
\[UIFont systemFontOfSize:(.*) weight:UIFontWeightBold\]  替换为  FontBold($2)
\[UIFont systemFontOfSize:(.*) weight:UIFontWeightSemibold\]  替换为  FontBold($2)
\[UIFont systemFontOfSize:(.*) weight:UIFontWeightMedium\]  替换为  FontMedium($2)
\[UIFont systemFontOfSize:(.*) weight:UIFontWeightRegular\]  替换为  FontRegular($2)
\[UIFont systemFontOfSize:(.*)\]\]  替换为 FontRegular($1)]
\[UIFont systemFontOfSize:(.*)\]\}  替换为 FontRegular($1)]}
\[UIFont systemFontOfSize:(.*)\];  替换为 FontRegular($1)];
\[UIFont systemFontOfSize:(\d*)  替换为 FontRegular($1)

=================================
20210314补充:
全局替换使用字体的代码之后,发现项目中很多使用了xib的页面,无法自动替换。在几个页面的awakeFromNib方法中重新设置了通用字体之后,发现修改起来工作量太大,考虑使用其他方式。

经与一个大神朋友讨教,提供了一个用runtime来替换字体的思路,值得一试。

此举主要是为了替换xib中的字体为我们想要修改成的字体,考虑重写几个可以设置字体的控件(UILabel、UIButton、UITextField和UITextView)的initWithCoder方法,在里面使用代码重新设置一遍字体,然后使用runtime交换UIFont的字体设置方法。

修改上面那几个宏定义:

#define sFontNameRegular    @"FZLTXHJW--GB1-0" // 常规体字体名称
#define sFontNameMedium     @"FZLTXHJW--GB1-0" // 中等字体名称
#define sFontNameBold       @"FZLTXHJW--GB1-0" // 粗体字体名称
#define sFontNameDINRegular @"DIN-Regular"     // DIN常规体字体名称
#define FontRegular(fontSize)    [UIFont fontWithName:sFontNameRegular size:fontSize]  // 常规体
#define FontMedium(fontSize)     [UIFont fontWithName:sFontNameMedium size:fontSize]   // 中等
#define FontBold(fontSize)       [UIFont fontWithName:sFontNameBold size:fontSize]     // 粗体
#define FontDINRegular(fontSize) [UIFont fontWithName:@"DIN-Regular" size:fontSize]         // DIN常规体

UIFont的category方法:

+ (void)load
{
    //交换systemFontOfSize: 方法
    [[self class] swizzleClassMethodWithOriginSel:@selector(systemFontOfSize:) swizzledSel:@selector(customSystemFontOfSize:)];
    //交换fontWithName:size:方法
    [[self class] swizzleClassMethodWithOriginSel:@selector(fontWithName:size:) swizzledSel:@selector(customFontWithName:size:)];
}

+ (UIFont *)customSystemFontOfSize:(CGFloat)fontSize
{
    return [UIFont customFontWithName:sFontNameRegular size:fontSize];
}

+ (UIFont *)customFontWithName:(NSString *)fontName size:(CGFloat)fontSize
{
    NSString *lowerFontName = [fontName lowercaseString];
    NSString *dinRegular = [sFontNameDINRegular lowercaseString];
    // 如果字体是sFontNameDINRegular(数字使用的字体),保留原有字体
    if ([lowerFontName isEqualToString:dinRegular]) {
        return [UIFont customFontWithName:fontName size:fontSize];
    }
    NSString *regular = [sFontNameRegular lowercaseString];
    NSString *medium = [sFontNameMedium lowercaseString];
    NSString *bold = [sFontNameBold lowercaseString];
    NSString *realFontName = fontName;
    if ([lowerFontName isEqualToString:regular]) {
        realFontName = sFontNameRegular;
    } else if ([lowerFontName isEqualToString:medium]) {
        realFontName = sFontNameMedium;
    } else if ([lowerFontName isEqualToString:bold]) {
        realFontName = sFontNameBold;
    } else {
        realFontName = sFontNameRegular;
    }
    
    return [UIFont customFontWithName:realFontName size:fontSize];
}

UILabel、UITextField和UITextView的category方法:

+ (void)load
{
    [[self class] swizzleInstanceMethodWithOriginSel:@selector(initWithCoder:) swizzledSel:@selector(customInitWithCoder:)];
}

- (instancetype)customInitWithCoder:(NSCoder *)coder
{
    if ([self customInitWithCoder:coder]) {
        self.font = [UIFont fontWithName:self.font.fontName size:self.font.pointSize];
    }
    return self;
}

UIButton的category方法:

+ (void)load
{
    [[self class] swizzleInstanceMethodWithOriginSel:@selector(initWithCoder:) swizzledSel:@selector(customInitWithCoder:)];
}

- (instancetype)customInitWithCoder:(NSCoder *)coder
{
    if ([self customInitWithCoder:coder]) {
        if (self.titleLabel != nil) {
            self.titleLabel.font = [UIFont fontWithName:self.titleLabel.font.fontName size:self.titleLabel.font.pointSize];
        }
    }
    return self;
}

经测试,此方法可行。大神就是大神,在此,感谢大神。

相关文章

网友评论

      本文标题:替换字体(正则替换->runtime替换)

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