TextView添加placeholder

TextView添加一个placeholder
效果图:


TextView添加placeholder_第1张图片
TextView.gif

这是使用:

    LvTextView * textV = [LvTextView textViewWithFrame:CGRectMake(100, 100, 200, 40) placeholder:@"这是一个placeholder" placeholderTextColor:[UIColor grayColor] font:16.0f];
    textV.layer.borderColor = [UIColor blackColor].CGColor;
    textV.layer.borderWidth = 1.0f;
    [self.view addSubview:textV];

创建一个继承TextView的类;
在此类的.h中;
用来描述placeholder的文字跟颜色

@property (nonatomic,strong) UIColor *placeholderTextColor;
@property (nonatomic,strong) NSString *placeholder;

主要创建TextView方法

    self.backgroundColor = [UIColor clearColor];
    UILabel * placeholderLabel = [[UILabel alloc] init];
    placeholderLabel.backgroundColor = [UIColor clearColor];
    placeholderLabel.numberOfLines = 0;
    [self addSubview:placeholderLabel];
    
    self.placeholderLabel = placeholderLabel;
    
    self.placeholderLabel.textColor = self.placeholderTextColor;
    self.font = [UIFont systemFontOfSize:15.0f];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];

然后重写一个父类属性的set方法,达到通过别的方法赋值也能正常使用,
最后在dealloc中释放一下UITextViewTextDidChangeNotification通知.
再加上awakeFromNib方法.
git已上传 https://github.com/SpTTlv/TextView-placeholder.git

你可能感兴趣的:(TextView添加placeholder)