美文网首页
iOS字体大小适配

iOS字体大小适配

作者: 樊二哈 | 来源:发表于2023-06-25 09:04 被阅读0次

    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
    }
    

    相关文章

      网友评论

          本文标题:iOS字体大小适配

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