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

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:54:50
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS應用開發中使UITextField實現placeholder屬性的方法,示例代碼為傳統的Objective-C語言,需要的朋友可以參考下
 

我們都知道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。感興趣的童鞋可以去試用一下。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 百色市| 阿拉尔市| 康平县| 壤塘县| 吉林省| 隆昌县| 南投市| 阆中市| 华宁县| 金坛市| 个旧市| 黄平县| 宁陕县| 稻城县| 沂水县| 三江| 辛集市| 涿鹿县| 沾益县| 嵊州市| 柞水县| 奉新县| 历史| 佛冈县| 库伦旗| 恩平市| 蓝田县| 文化| 同江市| 万年县| 惠安县| 成武县| 安陆市| 龙口市| 洛阳市| 石阡县| 盐津县| 渝中区| 泉州市| 新密市| 东宁县|