我們都曉得IOS開辟中的UITextField有個placeholder屬性,placeholder可以很便利引誘用戶輸出。然則UITextView卻沒有placeholder屬性。
1、鄙陋的辦法
若何讓UITextView也有placeholder功效呢?明天給列位分享一個比擬鄙陋的做法。思緒年夜概是如許的:
完成辦法:
1.創立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屬性設置成灰色,讓其看起來更像placeholder。
別忘了設置UITextView的署理,由於前面我們要用到UITextView的兩個署理辦法。
2.開端編纂的署理辦法:
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView.text isEqualToString:@"jb51.net"]) {
textView.text = @"";
textView.textColor = [UIColor blackColor];
}
}
在開端編纂的署理辦法外面,斷定假如是UITextView的text的值是placeholder,那末,就清空text,而且把textColor設置成真實的內容色彩,假定是黑色。
3.停止編纂的署理辦法:
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView.text.length<1) {
textView.text = @"jb51.net";
textView.textColor = [UIColor grayColor];
}
}
在停止編纂的署理辦法裡,斷定假如UITextView的text值為空,那末,就要把須要設置的placeholder賦值給UITextView的text,而且將textColor屬性設置成灰色。
4.添加輕擊手勢
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithtarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; //點擊次數
tapGesture.numberOfTouchesRequired = 1; //點擊手指數
[self.view addGestureRecognizer:tapGesture];
//輕擊手勢觸發辦法
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
[self.view endEditing:YES];
}
至此,就很鄙陋的完成了placeholder功效。為了便利測試,我加了一個手勢。感化是用鍵盤消逝,如許可以測試停止編纂的時刻placeholder會不會顯示。
Demo地址:IOSStrongDemo
2、平日的辦法
接上去來看比擬平日的辦法,哈哈~那末,這一次我將簡略的封裝一個UITextView。暫且取名叫GGPlaceholderTextView,GG前綴看著有點率性的哈。
GGPlaceholderTextView簡介:
GGPlaceholderTextView也是對text操作,詳細邏輯以下:
繼續UITextView,並設置placeholder屬性:
注冊開端編纂和停止編纂告訴,然後對text做響應的操作
經由過程UIApplicationWillTerminateNotification告訴,在APP加入的時刻移除告訴。
我把GGPlaceholderTextView寫鄙人面。不外,微信裡看代碼照樣不太便利,我曾經把代碼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
以上就是關於GGPlaceholderTextView的完成,假如你有相似需求,直接拿去用吧!詳細用法請往下看。
理論:
GGPlaceholderTextView *textView = [[GGPlaceholderTextView alloc] initWithFrame:CGRectMake(0, 64, SCREEN.width , 200)];
textView.backgroundColor = [UIColor whiteColor];
textView.placeholder = @"jb51.net";
[self.view addSubview:textView];
經由封裝後的GGPlaceholderTextView,應用起來是否是跟UITextField異常類似。固然,我封裝的比擬簡略,github上也有一些同伙封裝帶placeholder屬性的UITextView。好比:TextViewPlaceholder。感興致的童鞋可以去試用一下。
【iOS運用開辟中使UITextField完成placeholder屬性的辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!