iOS10字体展示不开问题及其他问题汇总(见相关链接)

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不是用来装逼的,理解并合理运用,会有出乎意料的效果。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容