iOS字体大小适配

方案一

#import "UIFont+runtime.h"
#import 
#define UI_SCREEN  375 // UI设计原型图的手机尺寸宽度(6), 6p的--414
@implementation UIFont (runtime)
+ (void)load {
    // 获取替换后的类方法
    Method newMethod1 = class_getClassMethod([self class], @selector(adjustSystemFontOfSize:));
    // 获取替换前的类方法
    Method method1 = class_getClassMethod([self class], @selector(systemFontOfSize:));
    // 然后交换类方法,交换两个方法的IMP指针,(IMP代表了方法的具体的实现)
    method_exchangeImplementations(newMethod1, method1);
    
    
    // 获取替换后的类方法
    Method newMethod2 = class_getClassMethod([self class], @selector(adjustFontWithName:size:));
    // 获取替换前的类方法
    Method method2 = class_getClassMethod([self class], @selector(fontWithName:size:));
    // 然后交换类方法,交换两个方法的IMP指针,(IMP代表了方法的具体的实现)
    method_exchangeImplementations(newMethod2, method2);
    
    
    // 获取替换后的类方法
    Method newMethod3 = class_getClassMethod([self class], @selector(adjustSystemFontOfSize:weight:));
    // 获取替换前的类方法
    Method method3 = class_getClassMethod([self class], @selector(systemFontOfSize:weight:));
    // 然后交换类方法,交换两个方法的IMP指针,(IMP代表了方法的具体的实现)
    method_exchangeImplementations(newMethod3, method3);
    
    // 获取替换后的类方法
    Method newMethod4 = class_getClassMethod([self class], @selector(adjustBoldSystemFontOfSize:));
    // 获取替换前的类方法
    Method method4 = class_getClassMethod([self class], @selector(boldSystemFontOfSize:));
    // 然后交换类方法,交换两个方法的IMP指针,(IMP代表了方法的具体的实现)
    method_exchangeImplementations(newMethod4, method4);
    
    // 获取替换后的类方法
    Method newMethod5 = class_getClassMethod([self class], @selector(adjustItalicSystemFontOfSize:));
    // 获取替换前的类方法
    Method method5 = class_getClassMethod([self class], @selector(italicSystemFontOfSize:));
    // 然后交换类方法,交换两个方法的IMP指针,(IMP代表了方法的具体的实现)
    method_exchangeImplementations(newMethod5, method5);
    /*容易让UIAlertController  变形
    // 获取替换后的类方法
    Method newMethod6 = class_getClassMethod([self class], @selector(adjustFontWithDescriptor:size:));
    // 获取替换前的类方法
    Method method6 = class_getClassMethod([self class], @selector(fontWithDescriptor:size:));
    // 然后交换类方法,交换两个方法的IMP指针,(IMP代表了方法的具体的实现)
    method_exchangeImplementations(newMethod6, method6);
    */
}

+ (UIFont *)adjustSystemFontOfSize:(CGFloat)fontSize {
    UIFont *newFont = nil;
    newFont = [UIFont adjustSystemFontOfSize:[UIFont adjustFontSize:fontSize]];
    return newFont;
}
+ (UIFont *)adjustFontWithName:(NSString *)name size:(CGFloat)fontSize{
    UIFont *newFont = nil;
    newFont = [UIFont adjustFontWithName:name size:[UIFont adjustFontSize:fontSize]];
    return newFont;
}
+ (UIFont *)adjustSystemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight{
    UIFont *newFont = nil;
    newFont = [UIFont adjustSystemFontOfSize:[UIFont adjustFontSize:fontSize]];
    return newFont;
}
+ (UIFont *)adjustBoldSystemFontOfSize:(CGFloat)fontSize{
    UIFont *newFont = nil;
    newFont = [UIFont adjustBoldSystemFontOfSize:[UIFont adjustFontSize:fontSize]];
    return newFont;
}
+ (UIFont *)adjustItalicSystemFontOfSize:(CGFloat)fontSize{
    UIFont *newFont = nil;
    newFont = [UIFont adjustItalicSystemFontOfSize:[UIFont adjustFontSize:fontSize]];
    return newFont;
}

+ (UIFont *)adjustFontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)fontSize{
    UIFont *newFont = nil;
    newFont = [UIFont adjustFontWithDescriptor:descriptor size:[UIFont adjustFontSize:fontSize]];
    return newFont;
}
+ (CGFloat)adjustFontSize:(CGFloat)fontSize{
    return fontSize * [UIScreen mainScreen].bounds.size.width / UI_SCREEN;
}
@end

方案二

方案一中,如果使用地图之类,会产生字体超出控件的情况。
不妨在UIFont中用比例因子来做字体大小的适配。

UIFont.systemFont(ofSize: ConstUtil.fontScale(fontSize: 14), weight: .medium)
struct ConstUtil{
    /// 屏幕的宽
    static let winWidth = UIScreen.main.bounds.size.width
    /// 屏幕的高
    static let winHeight = UIScreen.main.bounds.size.height
    /// 缩放大小
    static var winScale: CGFloat{
        if winWidth == 320 {
            return 0.853
        }
        if winWidth == 414{
            return 1.104
        }
        return 1
    }
    /// 文字缩放
    static func fontScale(fontSize:CGFloat)->CGFloat{
        return fontSize * ConstUtil.winScale
    }
}

你可能感兴趣的:(iOS字体大小适配)