美文网首页
全局配置UIFont

全局配置UIFont

作者: iLeooooo | 来源:发表于2018-09-14 15:35 被阅读26次

    全局修改UIFont,根据屏幕适配字体大小

    #import "UILabel+YQFont.h"
    #import <objc/runtime.h>
    
    @implementation UILabel (YQFont)
    
    + (void)load {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = [self class];
            //替换三个方法
            SEL originalSelector = @selector(init);
            SEL originalSelector2 = @selector(initWithFrame:);
            SEL originalSelector3 = @selector(awakeFromNib);
            SEL swizzledSelector = @selector(YQBaseInit);
            SEL swizzledSelector2 = @selector(YQBaseInitWithFrame:);
            SEL swizzledSelector3 = @selector(YQBaseAwakeFromNib);
            
            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
            Method originalMethod3 = class_getInstanceMethod(class, originalSelector3);
            Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
            Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);
            Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3);
            BOOL didAddMethod =
            class_addMethod(class,
                            originalSelector,
                            method_getImplementation(swizzledMethod),
                            method_getTypeEncoding(swizzledMethod));
            BOOL didAddMethod2 =
            class_addMethod(class,
                            originalSelector2,
                            method_getImplementation(swizzledMethod2),
                            method_getTypeEncoding(swizzledMethod2));
            BOOL didAddMethod3 =
            class_addMethod(class,
                            originalSelector3,
                            method_getImplementation(swizzledMethod3),
                            method_getTypeEncoding(swizzledMethod3));
            
            if (didAddMethod) {
                class_replaceMethod(class,
                                    swizzledSelector,
                                    method_getImplementation(originalMethod),
                                    method_getTypeEncoding(originalMethod));
                
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
            if (didAddMethod2) {
                class_replaceMethod(class,
                                    swizzledSelector2,
                                    method_getImplementation(originalMethod2),
                                    method_getTypeEncoding(originalMethod2));
            }else {
                method_exchangeImplementations(originalMethod2, swizzledMethod2);
            }
            if (didAddMethod3) {
                class_replaceMethod(class,
                                    swizzledSelector3,
                                    method_getImplementation(originalMethod3),
                                    method_getTypeEncoding(originalMethod3));
            }else {
                method_exchangeImplementations(originalMethod3, swizzledMethod3);
            }
        });
        
    }
    /**
     *在这些方法中将你的字体名字换进去
     */
    - (instancetype)YQBaseInit
    {
        id __self = [self YQBaseInit];
        [self changeFont];
        return __self;
    }
    
    -(instancetype)YQBaseInitWithFrame:(CGRect)rect{
        id __self = [self YQBaseInitWithFrame:rect];
        [self changeFont];
        return __self;
    }
    
    -(void)YQBaseAwakeFromNib{
        [self YQBaseAwakeFromNib];
        [self changeFont];
    }
    
    - (void)changeFont {
        BOOL isBold = [self isBoldWithFontDescriptor:self.font.fontDescriptor];
        UIFont *font = isBold ? [UIFont boldSystemFontOfSize:RealFont(self.font.pointSize)] : [UIFont systemFontOfSize:RealFont(self.font.pointSize)];
        if (font) {
            self.font=font;
        }
    }
    
    // 获取Xib中字体是否是加粗字体
    - (BOOL)isBoldWithFontDescriptor:(UIFontDescriptor *)fontDesc {
        NSString *str = fontDesc.postscriptName;
        BOOL isBold = NO;
        if ([[str lowercaseString] containsString:@"bold"]) {
            isBold = YES;
        }
        return isBold;
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:全局配置UIFont

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