美文网首页
OC: UITextView + MaxMethod

OC: UITextView + MaxMethod

作者: 一欧Yiou | 来源:发表于2018-11-06 14:00 被阅读0次

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

    相关文章

      网友评论

          本文标题:OC: UITextView + MaxMethod

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