iOS字体大小适配

#define MyUIScreen  375 // UI设计原型图的手机尺寸宽度(6), 6p的--414

UIFont+runtime.m

#import"UIFont+runtime.h"

#import

@implementationUIFont(runtime)

+ (void)load {

    // 获取替换后的类方法

    Method newMethod = class_getClassMethod([self class], @selector(adjustFont:));

    // 获取替换前的类方法

    Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));

    // 然后交换类方法,交换两个方法的IMP指针,(IMP代表了方法的具体的实现)

    method_exchangeImplementations(newMethod, method);

}

+ (UIFont *)adjustFont:(CGFloat)fontSize {

    UIFont *newFont = nil;

    newFont = [UIFont adjustFont:fontSize * [UIScreen mainScreen].bounds.size.width/MyUIScreen];

    return newFont;

}

@end

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width, 60)];

label.text = @"iOS字体大小适配";

label.font = [UIFont systemFontOfSize:16];

[self.view addSubview:label];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容