首先应该写个runtime 控制下纯代码的字体
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method newMethod = class_getClassMethod([self class], @selector(adjustFont:));
Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
method_exchangeImplementations(newMethod, method);
Method newBoldMethod = class_getClassMethod([self class], @selector(adjustBoldFont:));
Method boldMethod = class_getClassMethod([self class], @selector(boldSystemFontOfSize:));
method_exchangeImplementations(newBoldMethod, boldMethod);
});
}
+ (UIFont *)adjustFont:(CGFloat)fontSize
{
[self adjustFont:fontSize];
UIFont *newFont = nil;
if (iPhone6 || iPhoneX)
{
//iPhone 6|6S|7|7S|8
newFont = [UIFont adjustFont:fontSize + 2];
}
else if (iPhone6Plus)
{
//iPhone 6P|7P|8P
newFont = [UIFont adjustFont:fontSize + 4];
}
else
{
//iPhone 5|5S|5C|4|4S
newFont = [UIFont adjustFont:fontSize];
}
// newFont = [UIFont adjustFont:fontSize * SizeScale];
return newFont;
}
+ (UIFont *)adjustBoldFont:(CGFloat)fontSize {
[self adjustBoldFont:fontSize];
UIFont *newFont = nil;
if (iPhone6 || iPhoneX)
{
//iPhone 6|6S|7|7S|8
newFont = [UIFont adjustBoldFont:fontSize + 2];
}
else if (iPhone6Plus)
{
//iPhone 6P|7P|8P
newFont = [UIFont adjustBoldFont:fontSize + 4];
}
else
{
//iPhone 5|5S|5C|4|4S
newFont = [UIFont adjustBoldFont:fontSize];
}
return newFont;
}
然后 nib字体控制 各种控件都写一遍吧
@implementation UILabel (FontSize)
+ (void)load {
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);
Method cmp = class_getInstanceMethod([self class], @selector(initWithFrame:));
Method myCmp = class_getInstanceMethod([self class], @selector(myInitWithFrame:));
method_exchangeImplementations(cmp, myCmp);
}
- (id)myInitWithCoder:(NSCoder*)aDecode {
[self myInitWithCoder:aDecode];
if (self.tag < TagNoScale) {
CGFloat fontSize = self.font.pointSize;
if (iPhone6 || iPhoneX)
{
//iPhone 6|6S|7|7S|8
self.font = [self.font fontWithSize:fontSize + 2];
}
else if (iPhone6Plus)
{
//iPhone 6P|7P|8P
self.font = [self.font fontWithSize:fontSize + 4];
}
else
{
//iPhone 5|5S|5C|4|4S
self.font = [self.font fontWithSize:fontSize];
}
}
return self;
}
- (id)myInitWithFrame:(CGRect)frame{
[self myInitWithFrame:frame];
if(self){
if (self.tag < TagNoScale) {
CGFloat fontSize = self.font.pointSize;
if (iPhone6 || iPhoneX)
{
//iPhone 6|6S|7|7S|8
self.font = [self.font fontWithSize:fontSize + 2];
}
else if (iPhone6Plus)
{
//iPhone 6P|7P|8P
self.font = [self.font fontWithSize:fontSize + 4];
}
else
{
//iPhone 5|5S|5C|4|4S
self.font = [self.font fontWithSize:fontSize];
}
}
}
return self;
}
@end
收工
偶尔myInitWithFrame会崩…… 仅供参考
网友评论