UITextField 天生就有 placeholder 屬性,非常好用;但 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];
别忘了設定 delegate,後面的兩個代理方法都要用到。
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView.text isEqualToString:@"jb51.net"]) {
textView.text = @"";
textView.textColor = [UIColor blackColor];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView.text.length < 1) {
textView.text = @"jb51.net";
textView.textColor = [UIColor grayColor];
}
}
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];
}
這個寫法缺點很明顯:提示文字硬編碼在代理方法裡,多個輸入框時會很痛苦。
改成繼承 UITextView,對外只暴露一個 placeholder 屬性,內部靠通知機制處理編輯狀態。
#import <UIKit/UIKit.h> @interface GGPlaceholderTextView : UITextView @property (nonatomic, strong) NSString *placeholder; @end
#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];
// App 結束時移除通知,避免殘留
[[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 *textView = [[GGPlaceholderTextView alloc] initWithFrame:CGRectMake(0, 64, SCREEN.width, 200)]; textView.backgroundColor = [UIColor whiteColor]; textView.placeholder = @"jb51.net"; [self.view addSubview:textView];
封裝之後,使用起來跟 UITextField 幾乎一樣直覺。更好的做法則是在 layoutSubviews 裡自行繪製 placeholder,或用 iOS 13 之後的 UITextView 直接指定 font 搭配自繪;GitHub 上也有 TextViewPlaceholder 這類現成方案可以參考。