自定义textView,增长placeholder属性

iOS文本输入框有两种:UITextField和UITextView。通常状况下,UITextField能够知足咱们开发的需求,输入文字,系统自带placeHolder属性,直接用点语法赋值就能够实现功能需求。布局

然而,有些时候咱们可能会用到UITextView,系统提供的UITextView是没有自带placeHolder属性的。想要实现placeHolder样式有至少两种方法:字体

1.添加一个label看成placeHolder的载体。实现UITextViewDelegate方法,监听文本输入框的输入状态,动态隐藏Labelatom

2.自定义UITextView,给UITextView添加一个placeHolder属性。简单方便地利用点语法直接赋值spa

 

我的比较推崇第二种方法,其有点是:自定义一次,受用无穷,之后任何地方须要用到UITextView的placeHolder属性,直接建立就能够用,简单暴力。code

下面开始咱们的自定义UITextView:(急用的能够直接粘代码)server

 

1、建立UITextView的子类,添加placeHolder属性(.h文件)对象

@interface JQTextView : UITextView
//placeHolder的文字
@property(nonatomic,copy)NSString* placeHolderText;
//placeHolder的颜色
@property(nonatomic,strong)UIColor* placeHolderColor;

@end

2、在重写初始化方法的时候,建立出来placeHolder的载体label。因此须要先写出全局属性(.m文件)blog

#import "JQTextView.h"
@interface JQTextView()

//能够改变隐藏状态的label
@property(nonatomic,strong)UILabel* placeHolderLabel;

@end

 

3、重写初始化方法,同时建立出来placeHolder的载体label。在这里可能须要直接发送通知,监听textview的变化开发

//重写init方法
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //建立label
        self.placeHolderLabel = [[UILabel alloc]init];
        //默认placeholder文字颜色
        self.placeHolderColor = [UIColor lightGrayColor];
        //能够换行
        self.placeHolderLabel.numberOfLines = 0;
        self.placeHolderLabel.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.placeHolderLabel];
        //发送通知,监听textview的变化
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textviewDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

#pragma mark - 监听textview变化
-(void)textviewDidChange{
    //系统属性,hasText(是否有文字)
    self.placeHolderLabel.hidden = self.hasText;
}

注意点:有添加就要有销毁,在dealloc方法中销毁,千万不要忘记呀!!!rem

 

4、重写layoutsubviews方法,配置label的frame

//须要动态设置label的高度
-(void)layoutSubviews{
    [super layoutSubviews];
    self.placeHolderLabel.mj_x = 5;
    self.placeHolderLabel.mj_y = 5;
    self.placeHolderLabel.mj_w = self.frame.size.width -10;
    //label高度判断
    CGSize maxSize = CGSizeMake(self.placeHolderLabel.mj_w, MAXFLOAT);
    
    /*
     NSString的对象方法,经过传入的参数返回一个CGRect数据,这个数据的size就是此时字符串显示成文本的尺寸
     options:
     一、NSStringDrawingUsesLineFragmentOrigin,整个文本将以每行组成的矩形为单位计算整个文本的尺寸
     二、NSStringDrawingUsesFontLeading,计算行高时使用行距
     三、NSStringDrawingUsesDeviceMetrics,计算布局时使用图元字形而不是印刷字体
     四、NSStringDrawingTruncatesLastVisibleLine,若是文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。若是没有指定NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略。
     */
    self.placeHolderLabel.mj_h = [self.placeHolderText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.placeHolderLabel.font} context:nil].size.height ;
    
}

 

5、重写setter方法

#pragma mark - 重写setter方法
-(void)setPlaceHolderText:(NSString *)placeHolderText{
    _placeHolderText = placeHolderText;
    
    self.placeHolderLabel.text = placeHolderText;
    
    [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 textviewDidChange];
}

-(void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    
    //调用通知方法
    [self textviewDidChange];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:UITextViewTextDidChangeNotification];
}

 

以上,直接用咱们自定义的TextView建立出来带有placeHolder属性的对象就能够了。点语法直接赋值:

//商品描述
    self.productTextView = [[JQTextView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    self.productTextView.placeHolderText = @"商品描述";
    
    self.productTextView.backgroundColor = [UIColor blackColor];
    
    self.productTextView.textColor = [UIColor whiteColor];
    [self.productView addSubview:self.productTextView];
相关文章
相关标签/搜索