手把手教你如何給TextView添加placeholder屬性

gif


UITextField 有個系統(tǒng)自帶的文本提示文字也就是placeholder屬性,大家應(yīng)該都知道,最常見的就用在搜索框上面的提醒文字。但是UITextField因?yàn)橹荒茌斎胍恍形淖值脑颍赡苡袝r候不能滿足我們的需求,這時候 UITextView 這個控件就派到用場了。但是UITextView雖然支持輸入多行文字,卻沒有像 UITextField那樣有個placeholder屬性給我們調(diào)用,本文就是教大家如何給? UITextView 增加一個 placeholder屬性。廢話不多少,我們開始:

雖然這個號早就不維護(hù)了,但是好多人有問題,所有又跑上來更新下!

時光飛逝啊,當(dāng)初寫下這篇文章的日子是 `2015.05.24 11:50`,想想現(xiàn)在自己還在 iOS 開發(fā)這條路上辛苦的爬著,廢話不多說,有興趣的去看看下面這個地址的 demo 吧:

https://github.com/ifelseboyxx/XXTextView











UITextField 有個系統(tǒng)自帶的文本提示文字也就是placeholder屬性,大家應(yīng)該都知道,最常見的就用在搜索框上面的提醒文字。但是UITextField因?yàn)橹荒茌斎胍恍形淖值脑颍赡苡袝r候不能滿足我們的需求,這時候 UITextView 這個控件就派到用場了。但是UITextView雖然支持輸入多行文字,卻沒有像 UITextField那樣有個placeholder屬性給我們調(diào)用,本文就是教大家如何給? UITextView 增加一個 placeholder屬性。廢話不多少,我們開始:

(一)我們先新建一個繼承自 UITextView 的 自定義控件 JSTextView。

? (二) ?然后在 - (instancetype)initWithFrame:(CGRect)frame 的里面初始化一個 UILabel。沒錯,我的思路就是用一個 UILabel 來實(shí)現(xiàn) placeholder 效果。具體代碼如下:

JSTextView.h 文件

這里我們暴露出 一個文字和文字顏色屬性,能夠方便我們使用的時候設(shè)置這些屬性!


#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; //設(shè)置可以輸入多行文字時可以自動換行

[self addSubview:placeholderLabel];

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

self.myPlaceholderColor= [UIColor lightGrayColor]; //設(shè)置占位文字默認(rèn)顏色

self.font= [UIFont systemFontOfSize:15]; //設(shè)置默認(rèn)的字體

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

}

return self;

}

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

這里我來解釋下: 這個UITextViewTextDidChangeNotification通知會監(jiān)聽UITextView文字的改變,也就是說只要文字一改變就會調(diào)用這個通知的方法,利用這個我們可以控制? 我們剛才在- (instancetype)initWithFrame:(CGRect)frame? 方法里面初始化的? UILabel 的顯示和隱藏:

#pragma mark -監(jiān)聽文字改變

- (void)textDidChange?{

self.placeholderLabel.hidden = self.hasText;

}

這個 hasText? 是一個 系統(tǒng)的 BOOL? 屬性,如果 UITextView 輸入了文字? hasText 就是 YES,反之就為 NO。

(三)我們在 ?- (void)layoutSubviews 方法里面設(shè)置 UILabel 的 frame.

- (void)layoutSubviews?{

[super layoutSubviews];

self.placeholderLabel.y=8; //設(shè)置UILabel 的 y值

self.placeholderLabel.x=5;//設(shè)置 UILabel 的 x 值

self.placeholderLabel.width=self.width-self.placeholderLabel.x*2.0; //設(shè)置 UILabel 的 x

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

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 文字的大小都用的下圖的一系列接口:

但是 這些方法已經(jīng)被蘋果遺棄了,并且出現(xiàn)了新的方法,也就是上面的那個方法。

圖1

(1)size參數(shù): 寬高限制,用于計(jì)算文本繪制時占據(jù)的矩形塊 。

(2)options參數(shù): NSStringDrawingOptions 大家可以看看這個 官方介紹。在多行的情況下 至少要包含 NSStringDrawingUsesLineFragmentOrigin NSStringDrawingUsesFontLeading ?這兩個屬性。

? (3) attributes參數(shù): 這是個字典? @{NSFontAttributeName : self.placeholderLabel.font} ?這里注意要成對的傳。

(4)context參數(shù) : 可以為 nil


(四)? 重寫我們 暴露出來的 文字 ,顏色以及系統(tǒng)的font 的 set方法 :

- (void)setMyPlaceholder:(NSString*)myPlaceholder{

_myPlaceholder= [myPlaceholder copy];

//設(shè)置文字

self.placeholderLabel.text= myPlaceholder;

//重新計(jì)算子控件frame

[self setNeedsLayout];

}

- (void)setMyPlaceholderColor:(UIColor*)myPlaceholderColor{

_myPlaceholderColor= myPlaceholderColor;

//設(shè)置顏色

self.placeholderLabel.textColor= myPlaceholderColor;

}

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

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

[super setFont:font];

self.placeholderLabel.font= font;

//重新計(jì)算子控件frame

[self setNeedsLayout];

}

(五) 重寫 - (void)setText:(NSString*)text? 以及 - (void)setAttributedText:(NSAttributedString*)attributedText 方法來控制 UILabel 的顯示 和 隱藏

- (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];

}

(7) 如何使用?

- (void)setUpTextView?{

JSTextView *textView = [[JSTextView alloc]initWithFrame:self.view.bounds];

[self.view addSubview:textView];

//1.設(shè)置提醒文字

textView.myPlaceholder=@"分享新鮮事...";

//2.設(shè)置提醒文字顏色

textView.myPlaceholderColor= [UIColorlightGrayColor];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容