自定義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];
相關文章
相關標籤/搜索