iOS---自定义带占位文字placeholder的TextView

来源:互联网 发布:福彩3d矩阵图 编辑:程序博客网 时间:2024/06/05 17:23

本篇文章主要介绍了"ios 自定义带占位文字placeholder的TextView",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下: 小伙伴们在写输入框的时候很可能需要带占位文字的TextView,由于TextField控件有占位文字的属性,但它不能多行显示,而TextView可以多行显示,但是它又没有占位文字属性,所以我们只能自定义TextView控件了:.




首先说一下思路:要定义这控件还是很简单,关键是一些细节上的处理问题(要让外部用起来很爽)。首先,让该控件继承自TextView,然后在这个TextView上加一个UILabel子控件,当用户点击TextView准备输入或TextView中有文字时,隐藏Label,没有文字时显示Label。当我们在监听TextView中的文字变化时,第一时间想到的肯定是设置代理,并让自己为代理,但这样的设计和苹果的代理模式相悖,能用但不合理这样设计不好,应该采取发通知的方式来监听TextView中的文字变化。


首先,初始化时加入Label:

- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [UIColor clearColor];                //添加一个显示占位文字的Label        UILabel *placeholderLabel = [[UILabel alloc]init];        placeholderLabel.backgroundColor = [UIColor clearColor];        placeholderLabel.numberOfLines = 0;        [self addSubview:placeholderLabel];                self.placeholderLabel = placeholderLabel;                //设置占位文字颜色        self.placeholderColor = [UIColor lightGrayColor];        //设置默认字体        self.font = [UIFont systemFontOfSize:15];                //设置监听通知,不要设置自己的代理为本身        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self];    }    return self;}

加入通知监听后,为防止内存泄露,要将其移除

/** *  移除通知监听 */- (void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self];}


布局子控件:

/** *  文字内容改变时调用的方法 */- (void)textChange {    self.placeholderLabel.hidden = (self.text.length != 0);}/** *  布局子控件 */- (void)<a target=_blank href="http://www.codes51.com/article/search_layoutSubviews/" target="_blank" style="color: rgb(0, 0, 0); text-decoration: none;"><strong>layoutSubviews</strong></a> {    [super <a target=_blank href="http://www.codes51.com/article/search_layoutSubviews/" target="_blank" style="color: rgb(0, 0, 0); text-decoration: none;"><strong>layoutSubviews</strong></a>];    CGFloat placeholderLabelX = 5;    CGFloat placeholderLabelY = 8;    CGFloat placeholderLabelWidth = self.frame.size.width - 2 * placeholderLabelX;    CGFloat placeholderLabelHeight = [self.placeholder boundingRectWithSize:CGSizeMake(placeholderLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font}  context:nil].size.height;    self.placeholderLabel.frame = CGRectMake(placeholderLabelX, placeholderLabelY, placeholderLabelWidth, placeholderLabelHeight);}- (void)setPlaceholder:(NSString *)placeholder {        //copy策略的话setter应该这样写    _placeholder = [placeholder copy];    self.placeholderLabel.text = placeholder;    //由于字符数变化,需要重新布局    [self setNeedsLayout];}- (void)setPlaceholderColor:(UIColor *)placeholderColor {    _placeholderColor = placeholderColor;    self.placeholderLabel.textColor = placeholderColor;}- (void)setFont:(UIFont *)font {    [super setFont:font];    self.placeholderLabel.font = font;    [self setNeedsLayout];}- (void)setText:(NSString *)text {    [super setText:text];        [self textChange];}

最后附上demo地址:http://download.csdn.net/detail/u013672551/9145287

以上就介绍了ios 自定义带占位文字placeholder的TextView,包括了方面的内容,希望对IOS开发有兴趣的朋友有所帮助。


0 0