美文网首页
iOS字体大小适配

iOS字体大小适配

作者: H了M | 来源:发表于2018-11-16 15:16 被阅读0次

#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];

相关文章

网友评论

      本文标题:iOS字体大小适配

      本文链接:https://www.haomeiwen.com/subject/mzjxgftx.html