国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS應用開發中使UITextField實現placeholder屬性的方法

2020-07-26 03:22:41
字體:
來源:轉載
供稿:網友

我們都知道iOS開發中的UITextField有個placeholder屬性,placeholder可以很方便引導用戶輸入。但是UITextView卻沒有placeholder屬性。

一、猥瑣的方法

如何讓UITextView也有placeholder功能呢?今天給各位分享一個比較猥瑣的做法。思路大概是這樣的:

  • 把UITextView的text當placeholder使用。
  • 在開始編輯的代理方法里清除placeholder。
  • 在結束編輯的代理方法里在設置placeholder。

實現方法:

1.創建UITextView:

復制代碼 代碼如下:

UITextView *textViewPlaceholder = [[UITextView alloc] initWithFrame:CGRectMake(20, 70, SCREEN.width - 40, 100)];
textViewPlaceholder.backgroundColor = [UIColor whiteColor];
textViewPlaceholder.text = @"VeVB.COm";
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:@"VeVB.COm"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}


在開始編輯的代理方法里面,判斷如果是UITextView的text的值是placeholder,那么,就清空text,并且把textColor設置成真正的內容顏色,假設是黑色。

3.結束編輯的代理方法:

復制代碼 代碼如下:

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length<1) {
        textView.text = @"VeVB.COm";
        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


二、通常的方法
接下來來看比較通常的方法,哈哈~那么,這一次我將簡單的封裝一個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 = @"VeVB.COm";
[self.view addSubview:textView];


經過封裝后的GGPlaceholderTextView,使用起來是不是跟UITextField非常相似。當然,我封裝的比較簡單,github上也有一些朋友封裝帶placeholder屬性的UITextView。比如:TextViewPlaceholder。感興趣的童鞋可以去試用一下。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵溪市| 锡林郭勒盟| 余江县| 南涧| 南陵县| 竹山县| 东乡| 伊通| 青岛市| 固阳县| 高邑县| 双流县| 泸溪县| 兴化市| 寿阳县| 公安县| 正镶白旗| 无锡市| 竹溪县| 晋州市| 星子县| 水城县| 弥渡县| 安平县| 平潭县| 于田县| 舟曲县| 黔南| 洪江市| 鄂伦春自治旗| 宁化县| 连江县| 宾阳县| 新化县| 平乐县| 永清县| 宜春市| 偃师市| 甘肃省| 榆树市| 女性|