您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UITextView 沒有 placeholder?兩種做法幫你補上

編輯:iOS 開發綜合

UITextField 天生就有 placeholder 屬性,一行程式碼就能給使用者提示。但 UITextView 偏偏沒有。如果你需要一個多行、帶提示文字的輸入框,就得自己動手。下面提供兩個方案:一個是快速土法煉鋼,一個是封装成可重複使用的元件。

方案一:拿 text 屬性來充當 placeholder

思路很直白:

  • 把 UITextView 的 text 當成 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];

初始化時順手把 textColor 設為灰色,視覺上就與真正的 placeholder 幾乎一致。別忘了設定 delegate,接下來兩個代理方法都要用到。

2. 開始編輯

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@"jb51.net"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}

判斷目前內容是否就是提示文字,是的話清空並改成真正的輸入顏色。

3. 結束編輯

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length < 1) {
        textView.text = @"jb51.net";
        textView.textColor = [UIColor grayColor];
    }
}

內容為空就把提示填回去。邏輯與 TextField 的 placeholder 行為一致。

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

這個做法缺點明顯:提示文字寫死在呼叫端,每個地方用都要重複一遍,而且邏輯與業務程式碼混在一起。適合臨時應急,不適合正式專案。

方案二:封裝成 GGPlaceholderTextView

把行為收攏到一個子類別裡,對外只暴露一個 placeholder 屬性,使用起來就和 UITextField 幾乎一樣。實作邏輯同樣是操作 text,但改用通知(Notification)取代代理,這樣就不會佔用呼叫端的 delegate 名額——這一點很重要,因為很多畫面上 delegate 已經被別的元件拿走了。

GGPlaceholderTextView.h

#import <UIKit/UIKit.h>

@interface GGPlaceholderTextView : UITextView
@property (nonatomic, strong) NSString *placeholder;
@end

介面乾淨得只剩一個屬性,這正是封裝該有的樣子。

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

三個註冊點各有作用:開始編輯、結束編輯,以及在 App 即將終止時把觀察者移除。最後這步常被忽略,但它是避免殘留註冊的關鍵收尾。

使用方式

GGPlaceholderTextView *textView = [[GGPlaceholderTextView alloc] initWithFrame:CGRectMake(0, 64, SCREEN.width, 200)];
textView.backgroundColor = [UIColor whiteColor];
textView.placeholder = @"jb51.net";
[self.view addSubview:textView];

還想再省事一點

網路上已有現成的開源方案,例如 TextViewPlaceholder 這類專案,把 placeholder 行為封裝得更完整,也處理了更多邊界情況。若你的需求只是「多行輸入框要有提示文字」,直接引入成熟套件往往比自己維護划算。

兩個方案的取捨很清楚:臨時 demo 用土法,正式專案用封裝。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved