iOS10字體寬度比之前多一個(gè)像素,高度不變,這個(gè)是個(gè)坑,需要注意一下!
這個(gè)問題會(huì)導(dǎo)致有些本來好的地方,到了iOS10就展示不開了。這里使用runtime做一個(gè)統(tǒng)一的處理。
#import "UIFont+MyFont.h"
#import@implementation UIFont (MyFont)
/**
*runtime注冊(cè)方法替換
*/
+(void)load{
//只執(zhí)行一次這個(gè)方法
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
// When swizzling a class method, use the following:
//獲取替換前的類方法
Method instance_eat =
class_getClassMethod(class, @selector(systemFontOfSize:));
//獲取替換后的類方法
Method instance_notEat =
class_getClassMethod(self, @selector(RoadSystemFontOfSize:));
//然后交換類方法
method_exchangeImplementations(instance_eat, instance_notEat);
});
}
+(UIFont *)RoadSystemFontOfSize:(CGFloat)fontSize{
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=10)? {
fontSize = fontSize * 0.95;
}
UIFont *newFont = [ UIFont preferredFontForTextStyle : UIFontTextStyleBody ];
UIFontDescriptor *ctfFont = newFont.fontDescriptor ;
NSString *fontString = [ctfFont objectForKey : @"NSFontNameAttribute"];
//使用 fontWithName 設(shè)置字體
return [UIFont fontWithName:fontString size:fontSize];
}
@end
寫在最后:
Runtime是實(shí)現(xiàn)OC動(dòng)態(tài)化的核心c庫,Runtime不是用來裝逼的,理解并合理運(yùn)用,會(huì)有出乎意料的效果。