Tips:
如果不弹出键盘
在启动模拟器的时候 菜单顶部有一个hardware按钮,里面有一个keyboard选项,点一下toggle blabla的那个选项,应该就行了
/*
* 初始化Textfiled
*
*/
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 40)];
//更改提示信息内容
tf.placeholder = @"";
//更改return显示效果
/*
UIReturnKeyDefault, -默认效果
UIReturnKeyGo, -开始
UIReturnKeyGoogle, -搜索
UIReturnKeyJoin, -加入
UIReturnKeyNext, -下一步
UIReturnKeySearch, -搜索
UIReturnKeySend, -发送
UIReturnKeyYahoo, -日本
UIReturnKeyDone, -结束
UIReturnKeyEmergencyCall,
*/
tf.returnKeyType = UIReturnKeyDone;
//键盘外观样式
/*
UIKeyboardAppearanceDefault, // Default apperance for the current input method.
UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceAlert
*/
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
//键盘类型
/*
UIKeyboardTypePhonePad - 电话键盘
UIKeyboardTypeNumberPad - 纯数字
UIKeyboardTypeEmailAddress - email地址键盘
*/
tf.keyboardType = UIKeyboardTypeASCIICapable;
//秘密输入
tf.secureTextEntry = YES;
//内容的对齐方式
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
tf.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//清空按键出现的时机
tf.clearButtonMode = UITextFieldViewModeAlways;
/*
UITextBorderStyleNone, -默认
UITextBorderStyleLine, -直线框
UITextBorderStyleRoundedRect -曲线框
*/
tf.borderStyle = UITextBorderStyleLine;
tf.delegate = self;
//自定义_clearButton(修改clearButton图片样式)
UIButton *clearButtonpassword = [tf valueForKey:@"clearButton"];
UIImage *image = [UIImage imageNamed:@"Login_icon_delete"];
[clearButtonpassword setImage:image forState:UIControlStateNormal];
self.tf = tf;
[self.view addSubview:tf];
}
/*
TextField - delegate
三个常用方法
textFieldShouldReturn - 取消响应事件
shouldChangeCharactersInRange - 修改文字
textFieldShouldEndEditing - 用户输入结束后,需要获得输入的内容
*/
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//点击returen之后的方法
//取消键盘输入
//[textField resignFirstResponder];
//输入用户名(这里的textField最好创建一个UIView类,便于复用)
if (textField == _usernametextField)
{
[_usernametextField resignFirstResponder];
[_passwordtextField becomeFirstResponder];
}
//输入密码
else if (textField == _passwordtextField)
{
[_passwordtextField resignFirstResponder];
if (!txtAuthCodetextField.hidden)//如果验证码框存在,弹出键盘
[txtAuthCodetextField becomeFirstResponder];
}
//输入验证码
else if (textField == txtAuthCodetextField)
{
[txtAuthCodetextField resignFirstResponder];
}
return YES;
}
//输入内容发生改变就会触发shouldChangeCharactersInRange
//包括输入,删除,粘贴内容
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"replacementString - %@",string);
return YES;
}
//用户输入结束后,需要获得输入的内容,在这里
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"textField - text - %@",textField.text);
return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 当捕捉到触摸事件时,取消UITextField的第一相应
[self removeResignFirstResponder];
}