您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

為 UITextView 加上 placeholder:土法煉鋼與優雅封裝兩種方案

編輯:iOS 相關教學

UITextField 天生就有 placeholder 屬性,非常好用;但 UITextView 沒有。當需要多行輸入框又想顯示提示文字時,就得自己動手。下面提供兩種做法:一種是快速可理解的直覺寫法,另一種是封裝成可重複使用的子類別。

方案一:直接操作 text 屬性

思路很單純:

  • 把 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];

别忘了設定 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];
    }
}

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

改成繼承 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 這類現成方案可以參考。

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