美文网首页
带placeholder的textView

带placeholder的textView

作者: lym不解释 | 来源:发表于2017-02-13 11:21 被阅读83次

YMTextView

带placeholder的textView

iOS开发中,UITextField和UITextView是最常用的文本输入的控件。
不同点:
1.UITextField继承自UIControl这个抽象类。
2.UITextView继承自UIScrollView这个实体类。

3.UITextView可以多行展示内容,并且还可以像UIScrollView一样滚动。
4.UITextField只能单独的展示一行内容。

5.UITextField中有一个placeholder属性,可以设置UITextField的占位文字。
6.UITextView并没有提供一个类似于placeholder这样的属性。

而开发中,单纯的UITextField或者UITextView都不能满足需求。这个时候就需要自己自定义一个带placeholder的textView;

方法有几种:
一、 最简单直接有效,并最容易想到的就是为textView添加一个placeholder属性,内部用一个UILable装载这个属性,通过监听UITextViewTextDidChangeNotification通知来改变UILable的显示隐藏。源码demo将发到github,连接https://github.com/yanmingLiu/YMTextView

二、 同样给UITextView添加placeholder和placeholderColor属性,添加通知监听文字改变,这里可以使用重写drawRect:方法来画一个placeholder,代码如下。

  这种方法注意点:
  1.每次调用setNeedsDisplay都会执行drawRect,就需要在drawRect方法里面判断如果有输入文字,就直接返回,不画占位文字
  2.重写相关属性的set方法
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 通知
        // 当UITextView的文字发生改变时,UITextView自己会发出一个UITextViewTextDidChangeNotification通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}


- (void)dealloc {
    [HWNotificationCenter removeObserver:self];
}

/**
 * 监听文字改变
 */
- (void)textDidChange {
    // 重绘(重新调用)
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // 如果有输入文字,就直接返回,不画占位文字
    if (self.hasText) return;
    
    // 文字属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.font;
    attrs[NSForegroundColorAttributeName] = self.placeholderColor?self.placeholderColor:[UIColor grayColor];
    // 画文字
//    [self.placeholder drawAtPoint:CGPointMake(5, 8) withAttributes:attrs];
    CGFloat x = 5;
    CGFloat w = rect.size.width - 2 * x;
    CGFloat y = 8;
    CGFloat h = rect.size.height - 2 * y;
    CGRect placeholderRect = CGRectMake(x, y, w, h);
    [self.placeholder drawInRect:placeholderRect withAttributes:attrs];
}

/**
* 重写相关属性的set方法
*/
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = [placeholder copy];
    [self setNeedsDisplay];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text {
    [super setText:text];
    // setNeedsDisplay会在下一个消息循环时刻,调用drawRect:
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont *)font {
    [super setFont:font];
    [self setNeedsDisplay];
}

三、通过runtime来修改

UITextView内部有一个“_placeHolderLabel”的私有成员变量,
参看一个类的所有属性的方法在https://github.com/yanmingLiu/GetClassInfo
这时我们可以通过KVC来访问私有变量。

- (void)setupTextView
{
    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:textView];

    // _placeholderLabel
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"我是占位符";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = [UIColor lightGrayColor];
    [placeHolderLabel sizeToFit];
    [textView addSubview:placeHolderLabel];
    
    // 通过KVC来访问私有变量
    [textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];

}

** YMTextView使用第一种方法实现,喜欢的给个star**
YMTextView连接:https://github.com/yanmingLiu/YMTextView
参看一个类的所有属性:https://github.com/yanmingLiu/GetClassInfo

相关文章

网友评论

      本文标题:带placeholder的textView

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