测试给UIlabel添加一个str属性
分类的.h文件
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UILabel (NSObject)
@property (nonatomic,copy) NSString *str;
@end
分类的.m文件
#import "UILabel+NSObject.h"
static const NSString *strKey = @"strKey";
@implementation UILabel (NSObject)
- (void)setStr:(NSString *)str {
self.text = str;
objc_setAssociatedObject(self, &strKey, str, OBJC_ASSOCIATION_COPY);
}
- (NSString *)str {
return objc_getAssociatedObject(self, &strKey);
}
@end
在VC中测试:
label.str = @"sss";
网友评论