iOS-Runtime字体适配

你还在为适配字体大小发愁?  看这里:

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

@implementation UIFont (Runtime)

+(void)load
{
    Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
    
    Method MyMethod = class_getClassMethod([self class], @selector(adapterFontOfSize:));
    
    method_exchangeImplementations(method, MyMethod);
}

+(UIFont *)adapterFontOfSize:(CGFloat)fontSize
{
    return [UIFont adapterFontOfSize:fontSize * [UIScreen mainScreen].bounds.size.width /MyUIScreen];
}

正常调用就行了:

    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width, 60)];
    label.text = @"适配字体大小";
    label.backgroundColor = [UIColor yellowColor];
    label.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:label];

我不信!  不信你可以试试.

Demo就不上了,就上面的代码.

原文地址:https://www.cnblogs.com/dianming/p/6689797.html