自定義一個具有placeholder的textView

有的時候在使用textView的時候需要設置一些placeholder, 但是發(fā)現(xiàn)textView并沒有類似textField的placeholder屬性,那么要想實現(xiàn)類似textField的placeholder效果需要自己自定義一個textView,

代碼如下

定義一個全局的placeholderLabel,用來顯示placeholder

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if(self) {

self.backgroundColor= [UIColor clearColor];

UILabel *placeholderLabel = [[UILabel alloc]init];//添加一個占位label

placeholderLabel.backgroundColor= [UIColor clearColor];

placeholderLabel.numberOfLines=0; //設置可以輸入多行文字時可以自動換行

[self addSubview:placeholderLabel];

self.placeholderLabel= placeholderLabel; //賦值保存

self.placeholderColor= [UIColor lightGrayColor]; //設置占位文字默認顏色

self.placeholderLabel.font= [UIFont systemFontOfSize:15]; //設置默認的字體

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:監(jiān)聽文字的改變

}

return self;

}

- (void)textDidChange

{

//hasText 輸入文字就是yes

self.placeholderLabel.hidden =? self.hasText;

}

- (void)layoutSubviews

{

[super layoutSubviews];

self.placeholderLabel.y = 8;

self.placeholderLabel.x=5;//設置 UILabel 的 x 值

self.placeholderLabel.width=self.width-self.placeholderLabel.x*2.0; //設置 UILabel 的 x

//根據(jù)文字計算高度

CGSize maxSize =CGSizeMake(self.placeholderLabel.width,MAXFLOAT);

self.placeholderLabel.height= [self.placeholder boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font} context:nil].size.height;

}

- (void)setPlaceholder:(NSString *)placeholder

{

_placeholder = [placeholder copy];

self.placeholderLabel.text = placeholder;

//重新計算子控件frame

[self setNeedsLayout];

}

- (void)setPlaceholderFont:(UIFont *)placeholderFont

{

_placeholderFont = placeholderFont;

self.placeholderLabel.font = _placeholderFont;

}

- (void)setPlaceholderColor:(UIColor *)placeholderColor

{

_placeholderColor = placeholderColor;

//設置顏色

self.placeholderLabel.textColor= placeholderColor;

}

//重寫這個set方法保持font一致

- (void)setFont:(UIFont*)font {

[super setFont:font];

self.placeholderLabel.font= font;

//重新計算子控件frame

[self setNeedsLayout];

}

- (void)setText:(NSString*)text{

[super setText:text];

[self textDidChange]; //這里調(diào)用的就是 UITextViewTextDidChangeNotification 通知的回調(diào)

}

- (void)setAttributedText:(NSAttributedString*)attributedText {

[super setAttributedText:attributedText];

[self textDidChange]; //這里調(diào)用的就是UITextViewTextDidChangeNotification 通知的回調(diào)

}

- (void)dealloc{

[[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];

}


demo下載地址:http://download.csdn.net/detail/qq_31927183/9553359

github:https://github.com/lwy121810/WYPlaceholderTextView

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容