//1、
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 260, 40)];
//2、
// textField.backgroundColor = [UIColor redColor];
//属性
//设置文字颜色:textColor
textField.textColor = [UIColor redColor];
//边框状态:borderStyle
/*
1、UITextBorderStyleRoundedRect 圆角
2、UITextBorderStyleLine 黑色直角边框
3、UITextBorderStyleBezel 灰色直角边框
4、UITextBorderStyleNone 无边框
*/
textField.borderStyle = UITextBorderStyleRoundedRect;
//文字水平对齐:textAlignment 默认左对齐
textField.textAlignment = NSTextAlignmentLeft;
//文字垂直对齐:contentVerticalAlignment 默认居中
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//设置字号:font
textField.font = [UIFont systemFontOfSize:20.0];
//自适应文字大小:adjustsFontSizeToFitWidth
textField.adjustsFontSizeToFitWidth = YES;
//设置最小字号:minimumFontSize
textField.minimumFontSize = 20.0;
//默认文字:text
textField.text = @"hello zhaoliang";
//提示文字:placeholder
textField.placeholder = @"请输入账号";
//密文:secureTextEntry
textField.secureTextEntry = NO;
//输入框是否可用:enabled
textField.enabled = YES;
//return键的类型:returnKeyType
/*
1、UIReturnKeyDefault return
2、UIReturnKeyDone Done
3、UIReturnKeyEmergencyCall EmergencyCall
4、UIReturnKeyGoogle=UIReturnKeyYahoo=UIReturnKeySearch Search
5、UIReturnKeyJoin Join
.
.
.
*/
textField.returnKeyType = UIReturnKeyJoin;
//键盘类型:keyboardType
/*
1、UIKeyboardTypeDefault 数字,符号,中英文
2、UIKeyboardTypeNumberPad 数字键盘
3、UIKeyboardTypeWebSearch 网址 .
4、UIKeyboardTypeURL .com / .
5、UIKeyboardTypeEmailAddress @ .
6、UIKeyboardTypeNumbersAndPunctuation 数字 符号
.
.
.
*/
textField.keyboardType = UIKeyboardTypeDefault;
//键盘颜色:keyboardAppearance
/*
1、UIKeyboardAppearanceDefault=UIKeyboardAppearanceLight 浅灰色
2、UIKeyboardAppearanceAlert=UIKeyboardAppearanceDark 深灰色
*/
textField.keyboardAppearance = UIKeyboardAppearanceDefault;
//背景图片:background 当边框状态为圆角时,背景图片无效
textField.background = [UIImage imageNamed:@"map.png"];
//一键清除按钮:clearButtonMode
/*
1、UITextFieldViewModeAlways 一直出现
2、UITextFieldViewModeNever 永不出现
3、UITextFieldViewModeWhileEditing 输入框编辑文字时出现,不编辑时消失
4、UITextFieldViewModeUnlessEditing 输入框编辑文字时消失,不编辑时出现
*/
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
//再次编辑是否清空之前的文字:clearsOnBeginEditing YES:清空
textField.clearsOnBeginEditing = YES;
//是否自动大写:autocapitalizationType
/*
1、UITextAutocapitalizationTypeAllCharacters 所有字母都大写
2、UITextAutocapitalizationTypeNone 所有字母都不大写
3、UITextAutocapitalizationTypeSentences 每个句子的首字母大写
4、UITextAutocapitalizationTypeWords 每个单词的首字母大写
*/
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
//是否自动纠错:autocorrectionType
/*
1、UITextAutocorrectionTypeDefault 默认
2、UITextAutocorrectionTypeNo 不纠错
3、UITextAutocorrectionTypeYes 纠错
*/
textField.autocorrectionType = UITextAutocorrectionTypeYes;
//设置左视图:leftView
//设置位置无效
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
view.backgroundColor = [UIColor redColor];
textField.leftView = view;
//左视图出现状态:leftViewMode
textField.leftViewMode = UITextFieldViewModeAlways;
//设置右视图:rightView
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
view2.backgroundColor = [UIColor yellowColor];
// textField.rightView = view2;
//右视图出现状态:rightViewMode
// textField.rightViewMode = UITextFieldViewModeAlways;
//不同的位置,不能用相同的view
//键盘上面的view:inputAccessoryView
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
view3.backgroundColor = [UIColor purpleColor];
textField.inputAccessoryView = view3;
//获取输入框里面的文字:textField.text
NSString *str = textField.text;
NSLog(@"%@",str);
/**************!!!!!!delegate!!!!!!!*************/
textField.delegate = self;
[textField becomeFirstResponder];
//3、
[self.view addSubview:textField];
1.2、代理方法
#pragma mark - UITextFieldDelegate
//return键的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//输入框失去响应:键盘收回,光标消失
[textField resignFirstResponder];
//输入框开始响应:键盘出现,光标出现
// [textField becomeFirstResponder];
return YES;
}
//一键删除按钮的方法
- (BOOL)textFieldShouldClear:(UITextField *)textField{
//返回YES:一键删除有效 返回NO:一键删除无效
if ([textField.text isEqualToString:@"123"]) {
return NO;
}
return YES;
}
//输入框是否可以开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
//输入框是否可以结束编辑 **
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if (textField.text.length < 11) {
return NO;
}
return YES;
}
//光标出现就开始执行
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
}
//光标消失就执行
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}