.h文件
#import <UIKit/UIKit.h>
@interface UITextView (MaxMethod)
// 站位字
- (void)setPlaceHolder:(NSString *)placeHolder;
@end
.m文件
#import "UITextView+MaxMethod.h"
#import <objc/runtime.h>
static NSString *Key_placeHolder = @"Key_placeHolder";
static UILabel *placeHolderLabel = nil;
@implementation UITextView (MaxMethod)
- (void)addPlaceHolderLabel:(NSString *)placeHolder {
if (!placeHolderLabel) {
placeHolderLabel = [[UILabel alloc] init];
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.textColor = [UIColor lightGrayColor];
[placeHolderLabel sizeToFit];
[self addSubview:placeHolderLabel];
placeHolderLabel.font = self.font;
[self setValue:placeHolderLabel forKey:@"_placeholderLabel"];
}
placeHolderLabel.text = placeHolder;
}
- (void)setPlaceHolder:(NSString *)placeHolder {
objc_setAssociatedObject(self, @selector(placeHolder), placeHolder, OBJC_ASSOCIATION_COPY);
[self addPlaceHolderLabel:placeHolder];
}
- (NSString *)placeHolder {
return objc_getAssociatedObject(self, _cmd);
}
@end
运用runtime
给分类添加属性
网友评论