美文网首页iOS 初探将来跳槽用牛叉的demo
iOS不同机型,文字大小按比例显示 runtime实现

iOS不同机型,文字大小按比例显示 runtime实现

作者: 东方_未明 | 来源:发表于2016-07-22 12:07 被阅读1072次

最近朋友和我聊天, 提到iOS在不同型号的手机上显示不同字体大小的问题, 不由自主的想到两个方法: 1. 全局适配的宏 2.hook UIFont方法
第一种方法比较简单 全局的宏, 这里手头一痒写了写第二种方法, 有什么不对的地方请指正, 万分感谢

#import "UIFont+GYFont.h"

/** 是否是4.0屏幕*/
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
/** 是否是4.7屏幕*/
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
/** 是否是5.5屏幕*/
#define iPhone6plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(1125, 2001), [[UIScreen mainScreen] currentMode].size) || CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size)) : NO)

#ifdef iPhone6
#define fontScale 1.0
#elif iPhone5
#define fontScale 0.8
#elif iPhone6plus
#define fontScale 1.2
#else
#define fontScale 0.7
#endif
#define displayFontSize(fontSize) fontSize * fontScale

@implementation NSObject (Extension)

+ (void)swizzleClassSelector:(SEL)originalSelector withSwizzledClassSelector:(SEL)swizzledSelector
{
    Method originalMethod = class_getClassMethod(self, originalSelector);
    Method swizzledMethod = class_getClassMethod(self, swizzledSelector);
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

@end



@implementation UIFont (GYFont)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleClassSelector:@selector(systemFontOfSize:) withSwizzledClassSelector:@selector(gy_systemFontOfSize:)];
        [self swizzleClassSelector:@selector(systemFontOfSize:weight:) withSwizzledClassSelector:@selector(gy_systemFontOfSize:weight:)];
        [self swizzleClassSelector:@selector(boldSystemFontOfSize:) withSwizzledClassSelector:@selector(gy_boldSystemFontOfSize:)];
        [self swizzleClassSelector:@selector(italicSystemFontOfSize:) withSwizzledClassSelector:@selector(gy_italicSystemFontOfSize:)];
        [self swizzleClassSelector:@selector(fontWithName:size:) withSwizzledClassSelector:@selector(gy_fontWithName:size:)];
    });
}

+ (UIFont *)gy_systemFontOfSize:(CGFloat)fontSize
{
    return [self gy_systemFontOfSize:displayFontSize(fontSize)];
}

+ (UIFont *)gy_systemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight
{
    return [self gy_systemFontOfSize:displayFontSize(fontSize) weight:weight];
}


+ (UIFont *)gy_boldSystemFontOfSize:(CGFloat)fontSize
{
    return [self gy_boldSystemFontOfSize:displayFontSize(fontSize)];
}

+ (UIFont *)gy_italicSystemFontOfSize:(CGFloat)fontSize
{
    return [self gy_italicSystemFontOfSize:displayFontSize(fontSize)];
}

+ (nullable UIFont *)gy_fontWithName:(NSString *)fontName size:(CGFloat)fontSize
{
    return [self gy_fontWithName:fontName size:displayFontSize(fontSize)];
}

相关文章

网友评论

  • xzm_prefect:同问:此方法是不是对代码创建和XIB创建都有用?
    NotFunGuy:对于xib创建的没有,怎么解决这个问题呢?如果是要在awakeFromNib方法里面手动修改,那不是特别麻烦,如果有100个页面,岂不是要喷血呀?楼主有好的解决办法么?
    东方_未明:@xzm_prefect 应该只适用于代码
    xzm_prefect:期待楼主回复
  • 屮艸芔:这个分类对xib创建的视图有用吗
  • d6c478020764:宏有问题.
    东方_未明:@咸鱼神003 我这里iPhone6, iPhone5是pch中的宏, 这里没有列举出来,是判断当前手机大小型号的, 如果你当前手机是iPhone6, 则代码中写的font不变, 如果当前手机是iPhone5则代码中的font 就乘以0.8了
    东方_未明:@咸鱼神003 哪里哪里? 求赐教 :heart_eyes:
    d6c478020764:@咸鱼神003 #ifdef iPhone6 只是判断 iPhone6这宏是否定义了. fontScale永远都是1.0
  • F森:学习了
    东方_未明:@F森 (づ ̄ 3 ̄)づ

本文标题:iOS不同机型,文字大小按比例显示 runtime实现

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