UITextField 有個系統自帶的文本提示文字也就是placeholder屬性,大家應該都知道,最常見的就用在搜索框上面的提醒文字。但是UITextField因為只能輸入一行文字的原因,可能有時候不能滿足我們的需求,這時候 UITextView 這個控件就派到用場了。但是UITextView雖然支持輸入多行文字,卻沒有像 UITextField那樣有個placeholder屬性給我們調用,本文就是教大家如何給? UITextView 增加一個 placeholder屬性。廢話不多少,我們開始:
雖然這個號早就不維護了,但是好多人有問題,所有又跑上來更新下!
時光飛逝啊,當初寫下這篇文章的日子是 `2015.05.24 11:50`,想想現在自己還在 iOS 開發這條路上辛苦的爬著,廢話不多說,有興趣的去看看下面這個地址的 demo 吧:
https://github.com/ifelseboyxx/XXTextView
UITextField 有個系統自帶的文本提示文字也就是placeholder屬性,大家應該都知道,最常見的就用在搜索框上面的提醒文字。但是UITextField因為只能輸入一行文字的原因,可能有時候不能滿足我們的需求,這時候 UITextView 這個控件就派到用場了。但是UITextView雖然支持輸入多行文字,卻沒有像 UITextField那樣有個placeholder屬性給我們調用,本文就是教大家如何給? UITextView 增加一個 placeholder屬性。廢話不多少,我們開始:
(一)我們先新建一個繼承自 UITextView 的 自定義控件 JSTextView。
? (二) ?然后在 - (instancetype)initWithFrame:(CGRect)frame 的里面初始化一個 UILabel。沒錯,我的思路就是用一個 UILabel 來實現 placeholder 效果。具體代碼如下:
JSTextView.h 文件
這里我們暴露出 一個文字和文字顏色屬性,能夠方便我們使用的時候設置這些屬性!
#import <UIKit/UIKit.h>
@interface JSTextView :UITextView
@property(nonatomic,copy) NSString *myPlaceholder; ?//文字
@property(nonatomic,strong) UIColor *myPlaceholderColor; //文字顏色
@end
JSTextView.m 文件
#import"JSTextView.h"
@interface JSTextView()
@property (nonatomic,weak) UILabel *placeholderLabel; //這里先拿出這個label以方便我們后面的使用
@end
- (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.myPlaceholderColor= [UIColor lightGrayColor]; //設置占位文字默認顏色
self.font= [UIFont systemFontOfSize:15]; //設置默認的字體
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:監聽文字的改變
}
return self;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:監聽文字的改變
這里我來解釋下: 這個UITextViewTextDidChangeNotification通知會監聽UITextView文字的改變,也就是說只要文字一改變就會調用這個通知的方法,利用這個我們可以控制? 我們剛才在- (instancetype)initWithFrame:(CGRect)frame? 方法里面初始化的? UILabel 的顯示和隱藏:
#pragma mark -監聽文字改變
- (void)textDidChange?{
self.placeholderLabel.hidden = self.hasText;
}
注:這個 hasText? 是一個 系統的 BOOL? 屬性,如果 UITextView 輸入了文字? hasText 就是 YES,反之就為 NO。
(三)我們在 ?- (void)layoutSubviews 方法里面設置 UILabel 的 frame.
- (void)layoutSubviews?{
[super layoutSubviews];
self.placeholderLabel.y=8; //設置UILabel 的 y值
self.placeholderLabel.x=5;//設置 UILabel 的 x 值
self.placeholderLabel.width=self.width-self.placeholderLabel.x*2.0; //設置 UILabel 的 x
//根據文字計算高度
CGSize maxSize =CGSizeMake(self.placeholderLabel.width,MAXFLOAT);
self.placeholderLabel.height= [self.myPlaceholder boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font} context:nil].size.height;
}?
注:- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary*)attributes context:(NSStringDrawingContext*)contextNS_AVAILABLE_IOS(7_0); 這個方法我來解釋下:
以前我們想要獲得 UILabel 文字的大小都用的下圖的一系列接口:
但是 這些方法已經被蘋果遺棄了,并且出現了新的方法,也就是上面的那個方法。
(1)size參數: 寬高限制,用于計算文本繪制時占據的矩形塊 。
(2)options參數: NSStringDrawingOptions 大家可以看看這個 官方介紹。在多行的情況下 至少要包含 NSStringDrawingUsesLineFragmentOrigin NSStringDrawingUsesFontLeading ?這兩個屬性。
? (3) attributes參數: 這是個字典? @{NSFontAttributeName : self.placeholderLabel.font} ?這里注意要成對的傳。
(4)context參數 : 可以為 nil 。
(四)? 重寫我們 暴露出來的 文字 ,顏色以及系統的font 的 set方法 :
- (void)setMyPlaceholder:(NSString*)myPlaceholder{
_myPlaceholder= [myPlaceholder copy];
//設置文字
self.placeholderLabel.text= myPlaceholder;
//重新計算子控件frame
[self setNeedsLayout];
}
- (void)setMyPlaceholderColor:(UIColor*)myPlaceholderColor{
_myPlaceholderColor= myPlaceholderColor;
//設置顏色
self.placeholderLabel.textColor= myPlaceholderColor;
}
//重寫這個set方法保持font一致
- (void)setFont:(UIFont*)font?{
[super setFont:font];
self.placeholderLabel.font= font;
//重新計算子控件frame
[self setNeedsLayout];
}
(五) 重寫 - (void)setText:(NSString*)text? 以及 - (void)setAttributedText:(NSAttributedString*)attributedText 方法來控制 UILabel 的顯示 和 隱藏
- (void)setText:(NSString*)text{
[super setText:text];
[self textDidChange]; //這里調用的就是 UITextViewTextDidChangeNotification 通知的回調
}?
- (void)setAttributedText:(NSAttributedString*)attributedText?{
[super setAttributedText:attributedText];
[self textDidChange]; //這里調用的就是UITextViewTextDidChangeNotification 通知的回調
}
(六) 最后別忘了銷毀 通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];
}
(7) 如何使用?
- (void)setUpTextView?{
JSTextView *textView = [[JSTextView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:textView];
//1.設置提醒文字
textView.myPlaceholder=@"分享新鮮事...";
//2.設置提醒文字顏色
textView.myPlaceholderColor= [UIColorlightGrayColor];