OC: UITextView + MaxMethod

.h文件

#import 

@interface UITextView (MaxMethod)
// 站位字
- (void)setPlaceHolder:(NSString *)placeHolder;

@end

.m文件

#import "UITextView+MaxMethod.h"
#import 

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给分类添加属性

你可能感兴趣的:(OC: UITextView + MaxMethod)