利用RunTime方法替换实现一键更换UILabel/UIButton/UIButtonLabel/UIBarButtonItem/UIBarItem所有字体

//

//  UILabel+Font.m

#import "UILabel+Font.h"

@interface UILabel (Font)

@end

@implementation UILabel (Font)

+ (void)load{


    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        Method setTextMethod = class_getInstanceMethod([self class], NSSelectorFromString(@"setText:"));

        MethodsetTextRunMethod =class_getInstanceMethod([selfclass],@selector(setTextRuntime:));

        method_exchangeImplementations(setTextMethod, setTextRunMethod);

        Method setTextMethod2 = class_getInstanceMethod([self class], NSSelectorFromString(@"setAttributedText:"));

        MethodsetTextRunMethod2 =class_getInstanceMethod([selfclass],@selector(setAttributedTextFont:));

        method_exchangeImplementations(setTextMethod2, setTextRunMethod2);

    });

}

- (void)setTextRuntime:(NSString*)text{

    [self setTextRuntime:text];

    [self setFontNew];

}

- (void)setAttributedTextFont:(NSAttributedString*)attributedText{

    [selfsetAttributedTextFont:attributedText];

    if(attributedText.length>0) {

        idmyObj = [[NSClassFromString(@"UIButtonLabel")alloc]init];

        if(![selfisKindOfClass:[myObjclass]]) {

//            NSLog(@"不是UIButtonLabel");

            [selfsetFontNew];

        }else{

            NSLog(@"是UIButtonLabel");

        }

    }

}

- (void)setFontNew{

    NSString*language = [DAConfiguserLanguage];

    intlanguage1 =0;

    if(!language) {

        NSString*pfLanguageCode = [NSLocalepreferredLanguages][0];

        if([pfLanguageCodehasPrefix:@"en"]) {

            language1 =1;

        }

    }

    if([languageisEqualToString:@"English"] || [languageisEqualToString:@"en"] || language1 ==1) {

        [self setFont:[UIFont fontWithName:@"Arciform" size:self.font.pointSize]];

    }

}

@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容