占位符文字的颜色
#define Placeholder @"placeholderLabel.textColor"
[self setValue:[UIColor grayColor] forKeyPath:Placeholder];
添加监听方法
[self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
代理也有相应的方法 可以在类里找到
还有通知的方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidBegin) name:UITextFieldTextDidBeginEditingNotification object:textField];// 参数
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidEnd) name:UITextFieldTextDidEndEditingNotification object:textField]; // 参数
// 实现的方法
- (void)editingDidBegin{
[self setValue:[UIColor whiteColor] forKeyPath:Placeholder];
}
- (void)editingDidEnd{
[self setValue:[UIColor grayColor] forKeyPath:Placeholder];
}
通过下面放大也可以监听
- (BOOL)becomeFirstResponder{
[self setValue:[UIColor whiteColor] forKeyPath:Placeholder];
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder{
[self setValue:[UIColor grayColor] forKeyPath:Placeholder];
return [super resignFirstResponder];
}