iOS應(yīng)用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法
我們都知道iOS開發(fā)中的UITextField有個placeholder屬性,placeholder可以很方便引導(dǎo)用戶輸入。但是UITextView卻沒有placeholder屬性。
一、猥瑣的方法
如何讓UITextView也有placeholder功能呢?今天給各位分享一個比較猥瑣的做法。思路大概是這樣的:
- 把UITextView的text當(dāng)placeholder使用。
- 在開始編輯的代理方法里清除placeholder。
- 在結(jié)束編輯的代理方法里在設(shè)置placeholder。
實現(xiàn)方法:
1.創(chuàng)建UITextView:
UITextView *textViewPlaceholder = [[UITextView alloc] initWithFrame:CGRectMake(20, 70, SCREEN.width - 40, 100)];
textViewPlaceholder.backgroundColor = [UIColor whiteColor];
textViewPlaceholder.text = @"jb51.net";
textViewPlaceholder.textColor = [UIColor grayColor];
textViewPlaceholder.delegate = self;
[self.view addSubview:textViewPlaceholder];
初始化UITextView,給UITextView的text賦值,并且給UITextView的textColor屬性設(shè)置成灰色,讓其看起來更像placeholder。
別忘了設(shè)置UITextView的代理,因為后面我們要用到UITextView的兩個代理方法。
2.開始編輯的代理方法:
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView.text isEqualToString:@"jb51.net"]) {
textView.text = @"";
textView.textColor = [UIColor blackColor];
}
}
在開始編輯的代理方法里面,判斷如果是UITextView的text的值是placeholder,那么,就清空text,并且把textColor設(shè)置成真正的內(nèi)容顏色,假設(shè)是黑色。
3.結(jié)束編輯的代理方法:
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView.text.length<1) {
textView.text = @"jb51.net";
textView.textColor = [UIColor grayColor];
}
}
在結(jié)束編輯的代理方法里,判斷如果UITextView的text值為空,那么,就要把需要設(shè)置的placeholder賦值給UITextView的text,并且將textColor屬性設(shè)置成灰色。
4.添加輕擊手勢
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; //點擊次數(shù)
tapGesture.numberOfTouchesRequired = 1; //點擊手指數(shù)
[self.view addGestureRecognizer:tapGesture];
//輕擊手勢觸發(fā)方法
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
[self.view endEditing:YES];
}
至此,就很猥瑣的實現(xiàn)了placeholder功能。為了方便測試,我加了一個手勢。作用是用鍵盤消失,這樣可以測試結(jié)束編輯的時候placeholder會不會顯示。
Demo地址:iOSStrongDemo
二、通常的方法
接下來來看比較通常的方法,哈哈~那么,這一次我將簡單的封裝一個UITextView。暫且取名叫GGPlaceholderTextView,GG前綴看著有點任性的哈。
GGPlaceholderTextView簡介:
GGPlaceholderTextView也是對text操作,具體邏輯如下:
繼承UITextView,并設(shè)置placeholder屬性:
注冊開始編輯和結(jié)束編輯通知,然后對text做相應(yīng)的操作
通過UIApplicationWillTerminateNotification通知,在APP退出的時候移除通知。
我把GGPlaceholderTextView寫在下面。不過,微信里看代碼還是不太方便,我已經(jīng)把代碼push到:iOSStrongDemo。你可以下載下來。
GGPlaceholderTextView.h
#import <UIKit/UIKit.h>
@interface GGPlaceholderTextView : UITextView
@property(nonatomic, strong) NSString *placeholder;
@end
定義placeholder屬性,類似于UITextField。
GGPlaceholderTextView.m
#import "GGPlaceholderTextView.h"
@implementation GGPlaceholderTextView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self addObserver];
}
return self;
}
- (id)init {
if (self = [super init]) {
[self addObserver];
}
return self;
}
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;
self.text = placeholder;
self.textColor = [UIColor grayColor];
}
-(void)addObserver
{
//注冊通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEndEditing:) name:UITextViewTextDidEndEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(terminate:) name:UIApplicationWillTerminateNotification object:[UIApplication sharedApplication]];
}
- (void)terminate:(NSNotification *)notification {
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didBeginEditing:(NSNotification *)notification {
if ([self.text isEqualToString:self.placeholder]) {
self.text = @"";
self.textColor = [UIColor blackColor];
}
}
- (void)didEndEditing:(NSNotification *)notification {
if (self.text.length<1) {
self.text = self.placeholder;
self.textColor = [UIColor grayColor];
}
}
@end
以上就是關(guān)于GGPlaceholderTextView的實現(xiàn),如果你有類似需求,直接拿去用吧!具體用法請往下看。
實踐:
GGPlaceholderTextView *textView = [[GGPlaceholderTextView alloc] initWithFrame:CGRectMake(0, 64, SCREEN.width , 200)];
textView.backgroundColor = [UIColor whiteColor];
textView.placeholder = @"jb51.net";
[self.view addSubview:textView];
經(jīng)過封裝后的GGPlaceholderTextView,使用起來是不是跟UITextField非常相似。當(dāng)然,我封裝的比較簡單,github上也有一些朋友封裝帶placeholder屬性的UITextView。比如:TextViewPlaceholder。感興趣的童鞋可以去試用一下。
- iOS UITextField最大字符數(shù)和字節(jié)數(shù)的限制詳解
- iOS中修改UITextField占位符字體顏色的方法總結(jié)
- IOS UI學(xué)習(xí)教程之設(shè)置UITextField各種屬性
- 解決iOS UITextField 編輯時文本偏移問題
- iOS開發(fā)之自定義UITextField的方法
- iOS UITextField、UITextView只限輸入中文、英文、數(shù)字及實時限制字符個數(shù)的封裝實現(xiàn)代碼
- 解決iOS7上UITextField限制字數(shù)輸入導(dǎo)致崩潰問題的方法
- IOS 中UITextField,UITextView,UILabel 根據(jù)內(nèi)容來計算高度
- iOS輸入框(UITextField)密碼明暗文切換方法
- iOS中監(jiān)聽UITextField值改變事件的方法實例
相關(guān)文章
TextField和TextView限制輸入字數(shù)長度
這篇文章主要為大家詳細介紹了TextField和TextView限制輸入字數(shù)長度代碼,感興趣的小伙伴們可以參考一下2016-08-08Objective-C優(yōu)雅使用KVO觀察屬性值變化
這篇文章主要為大家介紹了Objective-C優(yōu)雅使用KVO觀察屬性值變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08解決SDK注入權(quán)限驗證安卓正常,IOS出現(xiàn)config fail的方法
這篇文章主要介紹了解決SDK注入權(quán)限驗證安卓正常,IOS出現(xiàn)config fail的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06iOS中setValue和setObject的區(qū)別詳解
setObject:ForKey: 是NSMutableDictionary特有的;setValue:ForKey:是KVC的主要方法。接下來通過本文給大家分享iOS中setValue和setObject的區(qū)別,需要的朋友參考下2017-02-02