交換分類方法 Category Method Swizzling

最近設計這邊跟我說,我用的下拉刷新字體太粗,需要換成PingFang SC,我用的MJ的下拉刷新控件,我找了一下,發現下拉可以刷新和最後更新時間都用的同一個方法 + (instancetype)mj_label;

mj_label.png
實現.png

本來還想直接改了這個方法的,但是發現自己是pod進來的,所以就放棄更改的想法了,那麼最好的方法就是直接用runtime交換這個UILabel的分類方法。話不多說直接上代碼~

#import "RefreshHeader.h"
#import <objc/runtime.h>
#import <MJRefresh/MJRefreshComponent.h>

@implementation UILabel (WRRefresh)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        SEL selector = @selector(mj_label);
        NSString *newSelectorStr = [NSString stringWithFormat:@"wr_%@",NSStringFromSelector(selector)];
        // 通過 class_getClassMethod 獲取類方法 (UILabel的分類)
        Method originalMethod = class_getClassMethod([UILabel class], selector);
        Method newMethod = class_getClassMethod(self, NSSelectorFromString(newSelectorStr));
        method_exchangeImplementations(originalMethod, newMethod);
    });
}

+ (instancetype)wr_mj_label
{
    UILabel *label = [self wr_mj_label];
    label.font = [UIFont fontWithName:@"PingFang SC" size:14];
    return label;
}

@end


強烈推薦:超簡單!!! iOS設置狀態欄、導航欄按鈕、標題、顏色、透明度,偏移等

https://github.com/wangrui460/WRNavigationBar
https://github.com/wangrui460/WRNavigationBar_swift



歡迎關注我的微博:wangrui460

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章