UITextFile监听事件的几种方法总结

上一篇文章提到了如何修改UITextField的占位符文字颜色,接下来继续总结关于UITextFiled的监听事件的几个方法,同样,本文基于给定场景中:让占位符的颜色随着光标的切换而切换,如光标在手机号textField中,手机号显示为白色,而密码还是灰色,当切换到密码时,手机号为灰色,密码为白色。

效果如下图

使用addTarget方法

因为UITextField是继承自UIControl的,那么就可以使用addTarget方法来实现监听。

1
2
3
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding>

- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import "GYLLoginRegisterTextField.h"
#import "UITextField+GYLExtension.h"

@implementation GYLLoginRegisterTextField
- (void)awakeFromNib
{
self.tintColor = [UIColor whiteColor]; //设置光标颜色

//修改默认占位符文字颜色,使用category扩展UITextField,添加placeholderColor属性
self.placeholderColor = [UIColor grayColor];

[self addTarget:self action:@selector(beginEdit) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventEditingDidEnd];
}

- (void)beginEdit
{
self.placeholderColor = [UIColor whiteColor];
}

- (void)endEdit
{
self.placeholderColor = [UIColor grayColor];
}
@end

使用UITextFieldDelegate

使用代理时需要注意,代理只能设置一次,在自定义TextField已经设置代理,其他地方就设置其代理会出错。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#import "GYLLoginRegisterTextField.h"
#import "UITextField+GYLExtension.h"
@interface GYLLoginRegisterTextField () <UITextFieldDelegate>

@end

@implementation GYLLoginRegisterTextField
- (void)awakeFromNib
{
self.tintColor = [UIColor whiteColor]; //设置光标颜色

//修改默认占位符文字颜色
self.placeholderColor = [UIColor grayColor];

self.delegate = self;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.placeholderColor = [UIColor whiteColor];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.placeholderColor = [UIColor grayColor];
}

@end

使用NSNotificationCenter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import "GYLLoginRegisterTextField.h"
#import "UITextField+GYLExtension.h"
@interface GYLLoginRegisterTextField () <UITextFieldDelegate>
@end
@implementation GYLLoginRegisterTextField

- (void)awakeFromNib
{
self.tintColor = [UIColor whiteColor]; //设置光标颜色
//修改默认占位符文字颜色
self.placeholderColor = [UIColor grayColor];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEdit) name:UITextFieldTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEdit) name:UITextViewTextDidEndEditingNotification object:self];
}

- (void)beginEdit
{
self.placeholderColor = [UIColor whiteColor];
}

- (void)endEdit
{
self.placeholderColor = [UIColor grayColor];
}
@end

使用UITextField内部方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import "GYLLoginRegisterTextField.h"
#import "UITextField+GYLExtension.h"
@interface GYLLoginRegisterTextField ()

@end
@implementation GYLLoginRegisterTextField
- (void)awakeFromNib
{
self.tintColor = [UIColor whiteColor]; //设置光标颜色
//修改默认占位符文字颜色
self.placeholderColor = [UIColor grayColor];
}

- (BOOL)becomeFirstResponder
{
self.placeholderColor = [UIColor whiteColor];
return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder
{
self.placeholderColor = [UIColor grayColor];
return [super resignFirstResponder];
}
@end
文章目录
  1. 1. 使用addTarget方法
  2. 2. 使用UITextFieldDelegate
  3. 3. 使用NSNotificationCenter
  4. 4. 使用UITextField内部方法
,