美文网首页程序员
使用Runtime修改UILabel的setText方法

使用Runtime修改UILabel的setText方法

作者: 7890陈 | 来源:发表于2019-04-12 17:42 被阅读0次

    1、新建一个UILabel的Category(UILabel+AOP),添加一个桥梁方法

    // 设置一个桥梁方法
    - (void)setBrige:(NSString *)text
    {
        
    }
    

    2、使用runtime得到SEL、Method、SEL

        // 获取UILabel setText的selector、method、IMP
        SEL setTextSelector = @selector(setText:);
        Method setTextMethod = class_getInstanceMethod(clazz, setTextSelector);
        IMP setTextIMP = method_getImplementation(setTextMethod);
        
        // 获取替换原setText实现方法的hs_setText selector、method、IMP
        SEL hs_setTextSelector = @selector(hs_setText:);
        Method hs_setTextMethod = class_getInstanceMethod(clazz, hs_setTextSelector);
        IMP hs_setTextIMP = method_getImplementation(hs_setTextMethod);
        
        // 作为桥梁的方法
        SEL setBrigeSelector = @selector(setBrige:);
    

    3、先将桥梁方法的IMP 替换为UILabel的setText的IMP

    class_replaceMethod(clazz, setBrigeSelector, setTextIMP, method_getTypeEncoding(setTextMethod));
    

    4、再将UILabel的setText的IMP 替换为hs_setText

    class_replaceMethod(clazz, setTextSelector, hs_setTextIMP, method_getTypeEncoding(hs_setTextMethod));
    

    5、UILabel+AOP.m 完整代码

    #import "UILabel+AOP.h"
    #import <objc/runtime.h>
    
    @implementation UILabel (AOP)
    
    - (void)swizzleIMP
    {
        Class clazz = [self class];
        
        // 获取UILabel setText的selector、method、IMP
        SEL setTextSelector = @selector(setText:);
        Method setTextMethod = class_getInstanceMethod(clazz, setTextSelector);
        IMP setTextIMP = method_getImplementation(setTextMethod);
        
        // 获取替换原setText实现方法的hs_setText selector、method、IMP
        SEL hs_setTextSelector = @selector(hs_setText:);
        Method hs_setTextMethod = class_getInstanceMethod(clazz, hs_setTextSelector);
        IMP hs_setTextIMP = method_getImplementation(hs_setTextMethod);
        
        // 作为桥梁的方法
        SEL setBrigeSelector = @selector(setBrige:);
    //    Method setBrigeMethod = class_getInstanceMethod(clazz, setBrigeSelector);
    //    IMP setBrigeIMP = method_getImplementation(setBrigeMethod);
        
        // 将setBrige的IMP替换为setText的IMP
        class_replaceMethod(clazz, setBrigeSelector, setTextIMP, method_getTypeEncoding(setTextMethod));
        
        // 将setText的IMP 替换为 hs_setText的IMP
        class_replaceMethod(clazz, setTextSelector, hs_setTextIMP, method_getTypeEncoding(hs_setTextMethod));
    
    }
    
    + (void)initialize
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            id obj = [[self alloc] init];
            [obj swizzleIMP];
        });
    }
    
    - (void)hs_setText:(NSString *)text
    {
        // 可以在这里出来你想对text做的操作
        // ......
        [self setBrige:[NSString stringWithFormat:@"hs-%@",text]];
    }
    
    // 设置一个桥梁方法
    - (void)setBrige:(NSString *)text
    {
        
    }
    

    相关文章

      网友评论

        本文标题:使用Runtime修改UILabel的setText方法

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