給TextView添加placeholder屬性

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

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

JSTextView.h 文件

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

#import

@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

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

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

推薦閱讀更多精彩內容