UITextField->UIControl->UIView
常用属性
1.图片对象转化为颜色对象
textField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@”DOVE 1”]];
2.borderStyle 边框样式
textField.borderStyle = UITextBorderStyleNone;
3.contentVerticalAlignment 文本垂直方向
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
4.adjustsFontSizeToFitWidth 自适应文字大小
textField.adjustsFontSizeToFitWidth = YES;
5.minimumFontSize 最小字体
textField.minimumFontSize = 18;
6.keyboardAppearance 键盘外观
textField.keyboardAppearance = UIKeyboardAppearanceDark;
7.keyboardType 键盘样式
textField.keyboardType = UIKeyboardTypeDefault;
8.textColor 文字颜色
textField.textColor = [UIColor purpleColor];
9.returnKeyType return键样式
textField.returnKeyType = UIReturnKeyGo;
10.autocapitalizationType 字母大写样式
textField.autocapitalizationType =UITextAutocapitalizationTypeAllCharacters;
11.autocorrectionType 自动纠错
textField.autocorrectionType = NO;
12.placeholder 提示文字
textField.placeholder = @”屠龙宝刀,点击就送”;
13.secureTextEntry 是否密文显示
textField.secureTextEntry = YES;
14.clearButtonMode 一键删除的模式
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
15.clearsOnBeginEditing 再次编辑是否清空
textField.clearsOnBeginEditing = YES;
16.leftView 左视图
textField.leftView = view;
17.leftViewMode 左视图方式
textField.leftViewMode = UITextFieldViewModeAlways;
18.rightView 右视图
19.rightViewMode 右视图方式
20.inputAccessoryView 键盘上面的视图
21.inputView 键盘样式
22.textfield的代理
/**
* 输入框是否可以开始输入文字
*
* @param textField
*
* @return NO表示不能输入,YES表示可以输入
*/
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@”%s”,func);
return YES;
}// return NO to disallow editing.
/**
* 输入框开始响应就执行
*
* @param textField
*/
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@”%s”,func);
}// became first responder
//输入框是否可以结束输入文字
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
// if (textField.text.length !=2) {
// return NO;
// }
NSLog(@”%s”,func);
return YES;
}// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
/**
* 输入框结束响应就执行
*
* @param textField
*/
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@”%s”,func);
}// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
//
/**
* 输入框文字出现变化就执行的方法
*
* @param textField
* @param range
* @param string
*
* @return
*/
- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string{
NSLog(@"%ld %ld",range.length,range.location);
NSLog(@"%@",string);
return YES;
}// return NO to not change text
/**
* 一键删除
*
* @param textField
*
* @return NO表示删除无效
*/
- (BOOL)textFieldShouldClear:(UITextField *)textField{
if ([textField.text isEqualToString:@”123”]) {
return NO;
}
NSLog(@”%s”,func);
return YES;
}// called when clear button pressed. return NO to ignore (no notifications)
/**
* textFieldShouldReturn
*
* @param textField 传入的键盘
*
* @return
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//收回键盘:光标消失 输入框失去第一响应
[textField resignFirstResponder];
NSLog(@”%s”,func);
return YES;
}// called when ‘return’ key pressed. return NO to ignore.