美文网首页
iOS适配字体大小(自己常用的宏定义)

iOS适配字体大小(自己常用的宏定义)

作者: 阿龍飛 | 来源:发表于2017-06-13 09:31 被阅读518次

    在项目中肯定会遇到字体适配的问题,从上面可以看出:一般情况是在4,5,6上用一套字体大小,在6P上字体大小扩大1.5倍数。(淘宝字体是这样子的)

    方法一:用宏定义适配字体大小(根据手机机型判断)

    //宏定义
    
    #define FONT_SIZE(size) ([UIFont systemFontOfSize:FontSize(size))
    
    /**
     *  字体适配 我在PCH文件定义了一个方法
     */
    static inline CGFloat FontSize(CGFloat fontSize){
        if (SCREENWIDTH==320) {
            return fontSize-2;
        }else if (SCREENWIDTH==375){
            return fontSize;
        }else{
            return fontSize+2;
        }
    }
    

    方法二:用宏定义适配字体大小(根据手机尺寸判断)

    1.5代表6P尺寸的时候字体为1.5倍,5S和6尺寸时大小一样,也可根据需求自定义比例。

    代码如下:

    #define IsIphone6P          SCREEN_WIDTH==414
    #define SizeScale           (IsIphone6P ? 1.5 : 1)
    #define kFontSize(value)    value*SizeScale
    #define kFont(value)        [UIFont systemFontOfSize:value*SizeScale]
    

    方法三:用宏定义适配字体大小(根据手机尺寸判断)

    //不同屏幕尺寸字体适配
    #define kScreenWidthRatio  (UIScreen.mainScreen.bounds.size.width / 375.0)
    #define kScreenHeightRatio (UIScreen.mainScreen.bounds.size.height / 667.0)
    #define AdaptedWidth(x)  ceilf((x) * kScreenWidthRatio)
    #define AdaptedHeight(x) ceilf((x) * kScreenHeightRatio)
    #define AdaptedFontSize(R)     [UIFont systemFontOfSize:AdaptedWidth(R)]
    

    mine

    /* ********************************************************************* */
    
    // 判断是否为 iPhone 5SE
    #define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
    // 判断是否为iPhone 6/6s
    #define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
    // 判断是否为iPhone 6Plus/6sPlus
    #define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
    // 判断是否为iPhone X
    #define iPhoneX [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 812.0f
    
    /* ********************************************************************* */
    
    #define KWIDTH   [UIScreen mainScreen].bounds.size.width
    #define KHEIGHT  [UIScreen mainScreen].bounds.size.height
    
    //不同屏幕尺寸字体适配
    #define kScreenWidthRatio  (UIScreen.mainScreen.bounds.size.width / 375.0)
    #define kScreenHeightRatio (UIScreen.mainScreen.bounds.size.height / 667.0)
    #define AdaptedWidth(x)  ceilf((x) * kScreenWidthRatio)
    #define AdaptedHeight(x) ceilf((x) * kScreenHeightRatio)
    #define AdaptedFontSize(R)     [UIFont systemFontOfSize:AdaptedWidth(R)]
    
    // iPhone X  (安全区域 734 = 812 - kStatusBarHeight - KIndicatorHeight)
    // 状态栏高度
    #define kStatusBarHeight        [[UIApplication sharedApplication] statusBarFrame].size.height
    // tabBar高度
    #define kBottomBarHeight        (iPhoneX ? 83.f : 49.f)
    // 导航栏高度
    #define kNavigationBarHeight    (iPhoneX ? 88.f : 64.f)
    // home indicator
    #define KIndicatorHeight        (iPhoneX ? 34.f : 0.f)
    
    /* ********************************************************************* */
    
    /**
     * View 圆角和加边框
     */
    #define KViewBorderRadius(View, Radius, Width, Color)\
    \
    [View.layer setCornerRadius:(Radius)];\
    [View.layer setMasksToBounds:YES];\
    [View.layer setBorderWidth:(Width)];\
    [View.layer setBorderColor:[Color CGColor]]
    
    /**
     * View 圆角
     */
    #define KViewRadius(View, Radius)\
    \
    [View.layer setCornerRadius:(Radius)];\
    [View.layer setMasksToBounds:YES]
    
    /* ********************************************************************* */
    

    相关文章

      网友评论

          本文标题:iOS适配字体大小(自己常用的宏定义)

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