初始化textfield并设置位置及大小
1
| UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
|
把textfield加到视图中
1
| [self.window addSubview:text];
|
设置边框样式
只有设置了才会显示边框样式
1 2 3 4 5 6 7
| text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect } UITextBorderStyle;
|
设置输入框的背景颜色
此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉
1
| text.backgroundColor = [UIColor whiteColor];
|
设置背景
1 2 3
| text.background = [UIImage imageNamed:@"dd.png"];
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
|
Placeholder
1
| text.placeholder = @"password";
|
设置输入框内容的字体样式和大小
1
| text.font = [UIFont fontWithName:@"Arial" size:20.0f];
|
设置字体颜色
1
| text.textColor = [UIColor redColor];
|
输入框中是否有个叉号
在什么时候显示,用于一次性删除输入框中的内容
1 2 3 4 5 6 7 8
| text.clearButtonMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, 重不出现 UITextFieldViewModeWhileEditing, 编辑时出现 UITextFieldViewModeUnlessEditing, 除了编辑外都出现 UITextFieldViewModeAlways 一直出现 } UITextFieldViewMode;
|
输入框设置文字
1
| text.text = @"一开始就在输入框的文字";
|
密文输入
1
| text.secureTextEntry = YES;
|
是否纠错
1 2 3 4 5 6 7 8
| text.autocorrectionType = UITextAutocorrectionTypeNo; typedef enum { UITextAutocorrectionTypeDefault, 默认 UITextAutocorrectionTypeNo, 不自动纠错 UITextAutocorrectionTypeYes, 自动纠错 } UITextAutocorrectionType;
|
再次编辑就清空
1
| text.clearsOnBeginEditing = YES;
|
内容对齐方式
1 2 3 4 5 6
| text.textAlignment = UITextAlignmentLeft;
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
|
文本缩放
1 2 3 4
| textFied.adjustsFontSizeToFitWidth = YES;
text.minimumFontSize = 20;
|
设置键盘的样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| text.keyboardType = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault, 默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符 UIKeyboardTypeNumberPad, 数字键盘 UIKeyboardTypePhonePad, 电话键盘 UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名 UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点 UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType;
|
首字母是否大写
1 2 3 4 5 6 7 8
| text.autocapitalizationType = UITextAutocapitalizationTypeNone; typedef enum { UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写 } UITextAutocapitalizationType;
|
Return键
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| text.returnKeyType =UIReturnKeyDone; typedef enum { UIReturnKeyDefault, 默认 灰色按钮,标有Return UIReturnKeyGo, 标有Go的蓝色按钮 UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索 UIReturnKeyJoin,标有Join的蓝色按钮 UIReturnKeyNext,标有Next的蓝色按钮 UIReturnKeyRoute,标有Route的蓝色按钮 UIReturnKeySearch,标有Search的蓝色按钮 UIReturnKeySend,标有Send的蓝色按钮 UIReturnKeyYahoo,标有Yahoo的蓝色按钮 UIReturnKeyEmergencyCall, 紧急呼叫按钮 } UIReturnKeyType;
|
键盘外观
1 2 3 4 5
| textView.keyboardAppearance=UIKeyboardAppearanceDefault; typedef enum { UIKeyboardAppearanceDefault, 默认外观,浅灰色 UIKeyboardAppearanceAlert, 深灰 石墨色 } UIReturnKeyType;
|
最右侧加图片是以下代码
1 2 3 4 5 6 7 8 9 10 11
| UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]]; text.rightView=image; text.rightViewMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways } UITextFieldViewMode;
|
按return键键盘往下收
类要采用UITextFieldDelegate协议
1 2 3 4 5 6 7
| text.delegate = self; - (BOOL)textFieldShouldReturn:(UITextField *)textField { [text resignFirstResponder]; return YES; }
|
重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。
1 2 3 4 5 6 7 8 9 10
| – textRectForBounds: – drawTextInRect: – placeholderRectForBounds: – drawPlaceholderInRect: – borderRectForBounds: – editingRectForBounds: – clearButtonRectForBounds: – leftViewRectForBounds: – rightViewRectForBounds:
|
委托方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField{ } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return NO; } - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ return YES; } - (BOOL)textFieldShouldClear:(UITextField *)textField{ return YES; } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ return YES; }
|
通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
1 2 3
| UITextFieldTextDidBeginEditingNotification UITextFieldTextDidChangeNotification UITextFieldTextDidEndEditingNotification
|
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
1 2 3 4
| UIKeyboardWillShowNotification UIKeyboardDidShowNotification UIKeyboardWillHideNotification UIKeyboardDidHideNotification
|
限制只能输入特定的字符
1 2 3 4 5 6 7 8
| (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; BOOL canChange = [string isEqualToString:filtered]; return canChange; }
|
上面那个NUMBERS是一个宏,可以在文件顶部定义:
1
| #define NUMBERS @”0123456789\n” (这个代表可以输入数字和换行,请注意这个\n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件,因为你自己限制不让输入\n,好惨,我在项目中才发现的。)
|
所以,如果你要限制输入英文和数字的话,就可以把这个定义为:
1 2
| #define kAlphaNum @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″。
|
当然,你还可以在以上方法return之前,做一提示的,比如提示用户只能输入数字之类的。如果你觉得有需要的话。
限制只能输入一定长度的字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; { if ([string isEqualToString:@"\n"]) { return YES; } NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (self.myTextField == textField) { if ([toBeString length] > 20) { textField.text = [toBeString substringToIndex:20]; UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超过最大字数不能输入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease]; [alert show]; return NO; } } return YES; }
|
限制只能输入正整数
工具类方法
1 2 3 4 5 6 7 8 9
| static func isInt(str:String) -> Bool{ for char in str.utf8 { if (char > "9".utf8.first || char < "0".utf8.first){ return false; } } return true; }
|
代理方法
1 2 3 4 5 6 7 8 9 10 11
| func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if(ZJ_StringUtils.isInt(string)){ var inputText = jineInput.text!; inputText += string;
return true; }else{ return false; } }
|