iOS 用runtime给button的Category写text属性,设置title

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wang_Bo_JustOne/article/details/80799695

头文件

#import <UIKit/UIKit.h>


@interface UIButton (tool)

@property (nonatomic , copy)NSString *text;

@end

.m文件


#import "UIButton+tool.h"

#import <objc/runtime.h>

@implementation UIButton (tool)

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

    

 objc_setAssociatedObject(self, @"text", text, OBJC_ASSOCIATION_COPY);

    [self setTitle:text forState:UIControlStateNormal];

}


-(NSString *)text{

    return objc_getAssociatedObject(self,@"text");


}

@end


调用

button.text = @"获取";

亲测有效


猜你喜欢

转载自blog.csdn.net/wang_Bo_JustOne/article/details/80799695