iOS字体大小适配

1、改变代码中通过接口[UIFont systemFontOfSize:]、[UIFont systemFontOfSize:weight:]设置的字体,其他接口设置的字体可自行添加。建议项目中用统一的接口设置字体,方便适配。

2、改变xib文件中设置fitDeviceFont属性为true的字体。控件包括UILabel,UIButton,UITextField,UITextView。

其他控件可自行添加。

3、根据屏幕适配字体大小,适配公式可自行定义(当前代码是通过屏幕宽度比例,以414屏宽为基准,来修改字体大小),修改[UIFont fitFontSize:]函数即可。

文件:UIFont+FitDevice.h

@interface UIFont (FitDevice)
+ (CGFloat)fitFontSize:(CGFloat)curFontSize;
@end
#import "UIFont+FitDevice.h"
#import <objc/runtime.h>

const CGFloat default_screen_width = 414.0;

@implementation UIFont (FitDevice)
+ (void)load{
    Method nm1 = class_getClassMethod([self class], @selector(fitDeviceFont:));
    Method m1 = class_getClassMethod([self class], @selector(systemFontOfSize:));
    method_exchangeImplementations(nm1, m1);
    
    Method nm2 = class_getClassMethod([self class], @selector(fitDeviceFont:weight:));
    Method m2 = class_getClassMethod([self class], @selector(systemFontOfSize:weight:));
    method_exchangeImplementations(nm2, m2);
}

+ (UIFont*)fitDeviceFont:(CGFloat)fontSize{
    CGFloat newSize = [UIFont fitFontSize:fontSize];
    UIFont* font = [UIFont fitDeviceFont:newSize];
    return  font;
}

+ (UIFont*)fitDeviceFont:(CGFloat)fontSize weight:(UIFontWeight)weight{
    CGFloat newSize = [UIFont fitFontSize:fontSize];
    UIFont* font = [UIFont fitDeviceFont:newSize weight:weight];
    return  font;
}

+ (CGFloat)fitFontSize:(CGFloat)curFontSize{
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat font_size = curFontSize*screenW/default_screen_width;
//    if (screenW > default_screen_width) {
//        font_size += 1;
//    }else {
//        font_size -= 1;
//    }
    return font_size;
}
@end

文件:UILabelExt.swift

extension UILabel{
    @IBInspectable var fitDeviceFont:Bool{
        get{
            return self.fitDeviceFont
        }
        
        set{
            if (newValue) {
                let fontAttributes = self.font.fontDescriptor.fontAttributes
                if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
                    self.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
                }
            }else{
                
            }
        }
    }
}

extension UIButton{
    @IBInspectable var fitDeviceFont:Bool{
        get{
            return self.fitDeviceFont
        }
        
        set{
            if (newValue) {
                if let fontAttributes = self.titleLabel?.font.fontDescriptor.fontAttributes {
                    if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
                        self.titleLabel?.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
                    }
                }
                
            }else{
                
            }
        }
    }
}

extension UITextField{
    @IBInspectable var fitDeviceFont:Bool{
        get{
            return self.fitDeviceFont
        }
        
        set{
            if (newValue) {
                if let fontAttributes = self.font?.fontDescriptor.fontAttributes {
                    if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
                        self.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
                    }
                }
                
            }else{
                
            }
        }
    }
}

extension UITextView{
    @IBInspectable var fitDeviceFont:Bool{
        get{
            return self.fitDeviceFont
        }
        
        set{
            if (newValue) {
                if let fontAttributes = self.font?.fontDescriptor.fontAttributes {
                    if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
                        self.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
                    }
                }
            }else{
                
            }
        }
    }
}


func changeFontAttributes(fontAttributes:[UIFontDescriptor.AttributeName : Any])->(UIFontDescriptor,CGFloat)?{
    var attributes = fontAttributes
    let font_size_num = fontAttributes[UIFontDescriptor.AttributeName.size] as? NSNumber
    let font_size = CGFloat(font_size_num?.floatValue ?? 0)
    let fit_Font_size = UIFont.fitSize(font_size)
    if font_size != fit_Font_size && fit_Font_size > 0 {
        attributes[UIFontDescriptor.AttributeName.size] = NSNumber.init(value:fit_Font_size)
        let descriptor = UIFontDescriptor.init(fontAttributes: fontAttributes)
        return (descriptor,fit_Font_size)
    }
    return nil
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #defineMyUIScreen375//UI设计原型图的手机尺寸宽度(6),6p的--414 UIFont+r...
    H了M阅读 646评论 0 0
  • 一、需求背景介绍 最近在梳理项目是发现,针对xib创建的控件字体大小使用了runtime替换initWithCod...
    Joshua520阅读 2,321评论 0 2
  • 方案一 方案二 方案一中,如果使用地图之类,会产生字体超出控件的情况。不妨在UIFont中用比例因子来做字体大小的适配。
    前年的邂逅_Jerry阅读 1,651评论 9 13
  • 在iOS开发中,有些公司对字体也有适配要求,为了让字体美观,所以在不同尺寸的屏幕上字体大小也要做到适配。自己总结了...
    小和大大阅读 2,926评论 4 3
  • 在iOS开发中,有些公司对字体也有适配要求,为了让字体美观,所以在不同尺寸的屏幕上字体大小也要做到适配。自己总结了...
    深蓝_S阅读 20,649评论 17 31