全局修改UIFont字體:(包括UILabel和UIButton液兽、UITextField和UITextView的font字體闰挡,目前只對(duì)systemFontOfSize和boldSystemFontOfSize兩種方法有效)
完整代碼如下,復(fù)制添加到項(xiàng)目中即可使用惰拱,不明白的地方看代碼注釋即可:
UIFont+FontChange
UIFont+FontChange.h
//
// UIFont+FontChange.h
// degulade
//
// Created by degulade on 2021/3/15.
//
import <UIKit/UIKit.h>
/// 默認(rèn)字體修改(systemFontOfSize、boldSystemFontOfSize、italicSystemFontOfSize)
@interface UIFont (FontChange)
@end
/// 修改UILabel和UIButton的lab字體咒循,對(duì)xib有效(xib加粗暫時(shí)待優(yōu)化)
@interface UILabel (FontChange)
@end
/// 修改UITextField和UITextView的lab字體,對(duì)xib有效(xib加粗暫時(shí)待優(yōu)化)
@interface UITextField (FontChange)
@end
UIFont+FontChange.m
//
// UIFont+FontChange.m
// degulade
//
// Created by degulade on 2021/3/15.
//
// 自定義的正常字體
#define CustomFontName @"FZSJ-PIANPXHN"
// 自定義的粗字體
#define CustomBoldFontName @"AaKYTNon-CommercialUse"
#import "UIFont+FontChange.h"
#import <objc/runtime.h>
@implementation UIFont (FontChange)
+ (void)load {
// 線程安全
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
// 系統(tǒng)systemFontOfSize方法
Method systemMethod = class_getClassMethod(class, @selector(systemFontOfSize:));
// 自定義systemFontOfSize方法
Method swizzMethod = class_getClassMethod(class, @selector(my_systemFontOfSize:));
// 交換方法
method_exchangeImplementations(systemMethod, swizzMethod);
// boldSystemFontOfSize
Method systemMethod1 = class_getClassMethod(class, @selector(boldSystemFontOfSize:));
Method swizzMethod1 = class_getClassMethod(class, @selector(my_boldSystemFontOfSize:));
method_exchangeImplementations(systemMethod1, swizzMethod1);
// italicSystemFontOfSize
Method systemMethod2 = class_getClassMethod(class, @selector(italicSystemFontOfSize:));
Method swizzMethod2 = class_getClassMethod(class, @selector(my_italicSystemFontOfSize:));
method_exchangeImplementations(systemMethod2, swizzMethod2);
});
}
/// 自定義正常字體
/// @param size siz
+ (UIFont *)my_systemFontOfSize:(CGFloat)size {
[UIFont my_systemFontOfSize:size];
UIFont *font = [UIFont fontWithName:CustomFontName size:size];
if (font) {
return font;
}
// 字體缺失時(shí)防奔潰處理
return [UIFont systemFontOfSize:size];
}
/// 自定義加粗字體
/// @param size siz
+ (UIFont *)my_boldSystemFontOfSize:(CGFloat)size {
[UIFont my_boldSystemFontOfSize:size];
UIFont *font = [UIFont fontWithName:CustomBoldFontName size:size];
if (font) {
return font;
}
return [UIFont boldSystemFontOfSize:size];
}
+ (UIFont *)my_italicSystemFontOfSize:(CGFloat)size {
[UIFont my_italicSystemFontOfSize:size];
UIFont *font = [UIFont fontWithName:CustomFontName size:size];
if (font) {
return font;
}
return [UIFont italicSystemFontOfSize:size];
}
@end
@implementation UILabel (FontChange)
+ (void)load {
//方法交換應(yīng)該被保證绞愚,在程序中只會(huì)執(zhí)行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//獲得viewController的生命周期方法的selector
SEL systemSel = @selector(willMoveToSuperview:);
//自己實(shí)現(xiàn)的將要被交換的方法的selector
SEL swizzSel = @selector(myWillMoveToSuperview:);
//兩個(gè)方法的Method
Method systemMethod = class_getInstanceMethod([self class], systemSel);
Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
//首先動(dòng)態(tài)添加方法叙甸,實(shí)現(xiàn)是被交換的方法,返回值表示添加成功還是失敗
BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
if (isAdd) {
//如果成功位衩,說明類中不存在這個(gè)方法的實(shí)現(xiàn)
//將被交換方法的實(shí)現(xiàn)替換到這個(gè)并不存在的實(shí)現(xiàn)
class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
} else {
//否則裆蒸,交換兩個(gè)方法的實(shí)現(xiàn)
method_exchangeImplementations(systemMethod, swizzMethod);
}
});
}
- (void)myWillMoveToSuperview:(UIView *)newSuperview {
[self myWillMoveToSuperview:newSuperview];
if (self) {
/// 移除某個(gè)tag的自定義字體樣式
// 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
@implementation UITextField (FontChange)
+ (void)load {
//方法交換應(yīng)該被保證,在程序中只會(huì)執(zhí)行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//獲得viewController的生命周期方法的selector
SEL systemSel = @selector(willMoveToSuperview:);
//自己實(shí)現(xiàn)的將要被交換的方法的selector
SEL swizzSel = @selector(myWillMoveToSuperview:);
//兩個(gè)方法的Method
Method systemMethod = class_getInstanceMethod([self class], systemSel);
Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
//首先動(dòng)態(tài)添加方法糖驴,實(shí)現(xiàn)是被交換的方法僚祷,返回值表示添加成功還是失敗
BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
if (isAdd) {
//如果成功佛致,說明類中不存在這個(gè)方法的實(shí)現(xiàn)
//將被交換方法的實(shí)現(xiàn)替換到這個(gè)并不存在的實(shí)現(xiàn)
class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
} else {
//否則,交換兩個(gè)方法的實(shí)現(xiàn)
method_exchangeImplementations(systemMethod, swizzMethod);
}
});
}
- (void)myWillMoveToSuperview:(UIView *)newSuperview {
[self myWillMoveToSuperview:newSuperview];
if (self) {
/// 移除某個(gè)tag的自定義字體樣式
// 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