iOS10字體寬度比之前多一個像素,高度不變,這個是個坑,需要注意一下!
這個問題會導致有些本來好的地方,到了iOS10就展示不開了。這里使用runtime做一個統一的處理。
#import "UIFont+MyFont.h"
#import@implementation UIFont (MyFont)
/**
*runtime注冊方法替換
*/
+(void)load{
//只執行一次這個方法
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 設置字體
return [UIFont fontWithName:fontString size:fontSize];
}
@end
寫在最后:
Runtime是實現OC動態化的核心c庫,Runtime不是用來裝逼的,理解并合理運用,會有出乎意料的效果。