iOS UITextField获取或失去焦点方法
Posted st646889325
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS UITextField获取或失去焦点方法相关的知识,希望对你有一定的参考价值。
实现如下效果,点击“账号”或“密码”,横线颜色由灰色变成蓝色
代码如下:
UILabel *mNameTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 50, 40)];
mNameTitle.text = @"账号";
mNameTitle.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
mNameTitle.font = [UIFont systemFontOfSize:15];
mNameField = [[UITextField alloc] initWithFrame:CGRectMake(mNameTitle.frame.origin.x + mNameTitle.frame.size.width + 10, mNameTitle.frame.origin.y, KScreen_Width - 50 - mNameTitle.frame.size.width, mNameTitle.frame.size.height)];
mNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
mNameField.font = [UIFont systemFontOfSize:15];
mNameField.returnKeyType = UIReturnKeyDone;
mNameField.delegate = self;
mNameField.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
mNameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:@NSForegroundColorAttributeName: [CurrencyUtil hexStringToColor:@"#888888" Alpha:1.0f]];
[mNameField setSecureTextEntry:NO];
mNameLine = [[UIView alloc] init];
mNameLine.frame = CGRectMake(20, mNameField.frame.origin.y + mNameField.frame.size.height, KScreen_Width - 40, 0.5);
mNameLine.backgroundColor= [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];
UILabel *mPassTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, mNameLine.frame.origin.y + mNameLine.frame.size.height + 30, 50, 40)];
mPassTitle.text = @"密码";
mPassTitle.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
mPassTitle.font = [UIFont systemFontOfSize:15];
mPassField = [[UITextField alloc] initWithFrame:CGRectMake(mPassTitle.frame.origin.x + mPassTitle.frame.size.width + 10, mPassTitle.frame.origin.y, KScreen_Width - 80 - mPassTitle.frame.size.width, mPassTitle.frame.size.height)];
mPassField.clearButtonMode = UITextFieldViewModeWhileEditing;
mPassField.font = [UIFont systemFontOfSize:15];
mPassField.placeholder = @"请输入密码";
mPassField.returnKeyType = UIReturnKeyDone;
mPassField.delegate = self;
mPassField.textColor = [mUserUtil hexStringToColor:@"#333333" Alpha:1.0f];
[mPassField setSecureTextEntry:YES];
[mPassField setValue:[mUserUtil hexStringToColor:@"#888888" Alpha:1.0f] forKeyPath:@"placeholderLabel.textColor"];
mPassLine = [[UIView alloc] init];
mPassLine.frame = CGRectMake(20, mPassField.frame.origin.y + mPassField.frame.size.height, KScreen_Width - 40, 0.5);
mPassLine.backgroundColor= [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];
[self.view addSubview:mNameTitle];
[self.view addSubview:mNameField];
[self.view addSubview:mNameLine];
[self.view addSubview:mPassTitle];
[self.view addSubview:mPassField];
[self.view addSubview:mPassLine];
实现UITextFieldDelegate代理方法
// 获得焦点
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
if(textField == mNameField)
mNameLine.backgroundColor = [mUserUtil hexStringToColor:@"#005cac" Alpha:1.0f];
else if (textField == mPassField)
mPassLine.backgroundColor = [mUserUtil hexStringToColor:@"#005cac" Alpha:1.0f];
return YES;
// 失去焦点
- (void)textFieldDidEndEditing:(UITextField *)textField
if(textField == mNameField)
mNameLine.backgroundColor = [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];
else if (textField == mPassField)
mPassLine.backgroundColor = [mUserUtil hexStringToColor:@"#cccccc" Alpha:1.0f];
以上是关于iOS UITextField获取或失去焦点方法的主要内容,如果未能解决你的问题,请参考以下文章
按下按钮和文本字段切换后 UITextField 失去焦点(键盘保持可见)
自定义jqGrid编辑功能,当行获取焦点时编辑,失去焦点时保存