ios 自定义带占位文字(placeholder)的TextView


小伙伴们在写输入框的时候很可能需要带占位文字的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)layoutSubviews {
    [super layoutSubviews];
    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值