一、添加監聽對象
-(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];
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。