iOS自定義字體(二):全局字體修改

全局修改UIFont字體:(包括UILabel和UIButton、UITextField和UITextView的font字體,目前只對(duì)systemFontOfSize和boldSystemFontOfSize兩種方法有效)

完整代碼如下,復(fù)制添加到項(xiàng)目中即可使用,不明白的地方看代碼注釋即可:

UIFont+FontChange

UIFont+FontChange.h

//
//  UIFont+FontChange.h
//  degulade
//
//  Created by degulade on 2021/3/15.
//

import <UIKit/UIKit.h>
/// 默認(rèn)字體修改(systemFontOfSize、boldSystemFontOfSize、italicSystemFontOfSize)
@interface UIFont (FontChange)
@end

/// 修改UILabel和UIButton的lab字體,對(duì)xib有效(xib加粗暫時(shí)待優(yōu)化)
@interface UILabel (FontChange)
@end

/// 修改UITextField和UITextView的lab字體,對(duì)xib有效(xib加粗暫時(shí)待優(yōu)化)
@interface UITextField (FontChange)

@end

UIFont+FontChange.m

//
//  UIFont+FontChange.m
//  degulade
//
//  Created by degulade on 2021/3/15.
//

//  自定義的正常字體
#define CustomFontName @"FZSJ-PIANPXHN"
//  自定義的粗字體
#define CustomBoldFontName @"AaKYTNon-CommercialUse"

#import "UIFont+FontChange.h"
#import <objc/runtime.h>


@implementation UIFont (FontChange)

+ (void)load {
    
    //  線程安全
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class class = [self class];
        
        //  系統(tǒng)systemFontOfSize方法
        Method systemMethod = class_getClassMethod(class, @selector(systemFontOfSize:));
        //  自定義systemFontOfSize方法
        Method swizzMethod = class_getClassMethod(class, @selector(my_systemFontOfSize:));
        //  交換方法
        method_exchangeImplementations(systemMethod, swizzMethod);

        //  boldSystemFontOfSize
        Method systemMethod1 = class_getClassMethod(class, @selector(boldSystemFontOfSize:));
        Method swizzMethod1 = class_getClassMethod(class, @selector(my_boldSystemFontOfSize:));
        method_exchangeImplementations(systemMethod1, swizzMethod1);
        
        //  italicSystemFontOfSize
        Method systemMethod2 = class_getClassMethod(class, @selector(italicSystemFontOfSize:));
        Method swizzMethod2 = class_getClassMethod(class, @selector(my_italicSystemFontOfSize:));
        method_exchangeImplementations(systemMethod2, swizzMethod2);

    });
}


/// 自定義正常字體
/// @param size siz
+ (UIFont *)my_systemFontOfSize:(CGFloat)size {
    [UIFont my_systemFontOfSize:size];
    
    UIFont *font = [UIFont fontWithName:CustomFontName size:size];
    if (font) {
        return font;
    }
    //  字體缺失時(shí)防奔潰處理
    return [UIFont systemFontOfSize:size];
}

/// 自定義加粗字體
/// @param size siz
+ (UIFont *)my_boldSystemFontOfSize:(CGFloat)size {
    [UIFont my_boldSystemFontOfSize:size];

    UIFont *font = [UIFont fontWithName:CustomBoldFontName size:size];
    if (font) {
        return font;
    }
    return [UIFont boldSystemFontOfSize:size];
}


+ (UIFont *)my_italicSystemFontOfSize:(CGFloat)size {
    [UIFont my_italicSystemFontOfSize:size];

    UIFont *font = [UIFont fontWithName:CustomFontName size:size];
    if (font) {
        return font;
    }
    return [UIFont italicSystemFontOfSize:size];
}


@end




@implementation UILabel (FontChange)

+ (void)load {
    //方法交換應(yīng)該被保證,在程序中只會(huì)執(zhí)行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //獲得viewController的生命周期方法的selector
        SEL systemSel = @selector(willMoveToSuperview:);
        //自己實(shí)現(xiàn)的將要被交換的方法的selector
        SEL swizzSel = @selector(myWillMoveToSuperview:);
        //兩個(gè)方法的Method
        Method systemMethod = class_getInstanceMethod([self class], systemSel);
        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
        
        //首先動(dòng)態(tài)添加方法,實(shí)現(xiàn)是被交換的方法,返回值表示添加成功還是失敗
        BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            //如果成功,說明類中不存在這個(gè)方法的實(shí)現(xiàn)
            //將被交換方法的實(shí)現(xiàn)替換到這個(gè)并不存在的實(shí)現(xiàn)
            class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
        } else {
            //否則,交換兩個(gè)方法的實(shí)現(xiàn)
            method_exchangeImplementations(systemMethod, swizzMethod);
        }
        
    });
}

- (void)myWillMoveToSuperview:(UIView *)newSuperview {
    
    [self myWillMoveToSuperview:newSuperview];
    if (self) {
        /// 移除某個(gè)tag的自定義字體樣式
//        if (self.tag == 10086) {
//            self.font = [UIFont systemFontOfSize:self.font.pointSize];
//        } else {
            if ([UIFont fontNamesForFamilyName:CustomFontName])
                self.font  = [UIFont fontWithName:CustomFontName size:self.font.pointSize];
//        }
    }
}

@end




@implementation UITextField (FontChange)

+ (void)load {
    //方法交換應(yīng)該被保證,在程序中只會(huì)執(zhí)行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //獲得viewController的生命周期方法的selector
        SEL systemSel = @selector(willMoveToSuperview:);
        //自己實(shí)現(xiàn)的將要被交換的方法的selector
        SEL swizzSel = @selector(myWillMoveToSuperview:);
        //兩個(gè)方法的Method
        Method systemMethod = class_getInstanceMethod([self class], systemSel);
        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
        
        //首先動(dòng)態(tài)添加方法,實(shí)現(xiàn)是被交換的方法,返回值表示添加成功還是失敗
        BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            //如果成功,說明類中不存在這個(gè)方法的實(shí)現(xiàn)
            //將被交換方法的實(shí)現(xiàn)替換到這個(gè)并不存在的實(shí)現(xiàn)
            class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
        } else {
            //否則,交換兩個(gè)方法的實(shí)現(xiàn)
            method_exchangeImplementations(systemMethod, swizzMethod);
        }
        
    });
}

- (void)myWillMoveToSuperview:(UIView *)newSuperview {
    [self myWillMoveToSuperview:newSuperview];
    if (self) {
        /// 移除某個(gè)tag的自定義字體樣式
//        if (self.tag == 10086) {
//            self.font = [UIFont systemFontOfSize:self.font.pointSize];
//        } else {
            if ([UIFont fontNamesForFamilyName:CustomFontName])
                self.font  = [UIFont fontWithName:CustomFontName size:self.font.pointSize];
//        }
    }
}


@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容