iOS字体大小适配

2018-11-16  本文已影响0人  H了M

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

上一篇 下一篇

猜你喜欢

热点阅读