一、添加监听对象
-(void)awakeFromNib{
[super awakeFromNib];
[self addTarget:self action:@selector(textFildBegin) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(textFildEnd) forControlEvents:UIControlEventEditingDidEnd];
}
-(void)textFildBegin{
NSLog(@"begin");
}
-(void)textFildEnd{
NSLog(@"end");
}
二、代理
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet XMGLoginTextField *phoneNumber;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.phoneNumber.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"begin");
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"end");
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
三、通知
- (void)awakeFromNib
{
// 设置光标颜色
self.tintColor = [UIColor whiteColor];
// 设置默认的占位文字颜色
[self setValue:[UIColor grayColor] forKeyPath:XMGPlaceholderColorKey];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)beginEditing
{
[self setValue:[UIColor whiteColor] forKeyPath:XMGPlaceholderColorKey];
}
- (void)endEditing
{
[self setValue:[UIColor grayColor] forKeyPath:XMGPlaceholderColorKey];
}
@end
四、是否是键盘的第一响应用者
//键盘出现
-(BOOL)becomeFirstResponder{
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
return [super becomeFirstResponder];
}
//键盘退出
-(BOOL)resignFirstResponder{
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]}];
return [super resignFirstResponder];
}