UITextField即单行文本输入框,用法比较简单,创建好之后设置其属性,并管理获取焦点及失去焦点。
1、创建:
002 | UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; |
2、设置边框样式
005 | text.borderStyle = UITextBorderStyleRoundedRect; |
007 | UITextBorderStyleNone, |
008 | UITextBorderStyleLine, |
009 | UITextBorderStyleBezel, |
010 | UITextBorderStyleRoundedRect |
014 | text.backgroundColor = [UIColor whiteColor]; |
017 | text.background = [UIImage imageNamed:@ "dd.png" ]; |
020 | text.disabledBackground = [UIImage imageNamed:@ "cc.png" ]; |
022 | text.placeholder = @ "password" ; |
025 | text.font = [UIFont fontWithName:@ "Arial" size:20.0f]; |
028 | text.textColor = [UIColor redColor]; |
031 | text.clearButtonMode = UITextFieldViewModeAlways; |
034 | UITextFieldViewModeNever, 重不出现 |
035 | UITextFieldViewModeWhileEditing, 编辑时出现 |
036 | UITextFieldViewModeUnlessEditing, 除了编辑外都出现 |
037 | UITextFieldViewModeAlways 一直出现 |
038 | } UITextFieldViewMode; |
041 | text.text = @ "一开始就在输入框的文字" ; |
044 | text.secureTextEntry = YES; |
047 | text.autocorrectionType = UITextAutocorrectionTypeNo; |
050 | UITextAutocorrectionTypeDefault, 默认 |
051 | UITextAutocorrectionTypeNo, 不自动纠错 |
052 | UITextAutocorrectionTypeYes, 自动纠错 |
053 | } UITextAutocorrectionType; |
056 | text.clearsOnBeginEditing = YES; |
059 | text.textAlignment = UITextAlignmentLeft; |
062 | text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; |
065 | textFied.adjustsFontSizeToFitWidth = YES; |
068 | text.minimumFontSize = 20; |
071 | text.keyboardType = UIKeyboardTypeNumberPad; |
074 | UIKeyboardTypeDefault, 默认键盘,支持所有字符 |
075 | UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘 |
076 | UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 |
077 | UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符 |
078 | UIKeyboardTypeNumberPad, 数字键盘 |
079 | UIKeyboardTypePhonePad, 电话键盘 |
080 | UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名 |
081 | UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 |
082 | UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点 |
083 | UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符 |
084 | UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, |
088 | text.autocapitalizationType = UITextAutocapitalizationTypeNone; |
091 | UITextAutocapitalizationTypeNone, 不自动大写 |
092 | UITextAutocapitalizationTypeWords, 单词首字母大写 |
093 | UITextAutocapitalizationTypeSentences, 句子的首字母大写 |
094 | UITextAutocapitalizationTypeAllCharacters, 所有字母都大写 |
095 | } UITextAutocapitalizationType; |
098 | text.returnKeyType =UIReturnKeyDone; |
101 | UIReturnKeyDefault, 默认 灰色按钮,标有Return |
102 | UIReturnKeyGo, 标有Go的蓝色按钮 |
103 | UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索 |
104 | UIReturnKeyJoin,标有Join的蓝色按钮 |
105 | UIReturnKeyNext,标有Next的蓝色按钮 |
106 | UIReturnKeyRoute,标有Route的蓝色按钮 |
107 | UIReturnKeySearch,标有Search的蓝色按钮 |
108 | UIReturnKeySend,标有Send的蓝色按钮 |
109 | UIReturnKeyYahoo,标有Yahoo的蓝色按钮 |
110 | UIReturnKeyYahoo,标有Yahoo的蓝色按钮 |
111 | UIReturnKeyEmergencyCall, 紧急呼叫按钮 |
115 | textView.keyboardAppearance=UIKeyboardAppearanceDefault; |
117 | UIKeyboardAppearanceDefault, 默认外观,浅灰色 |
118 | UIKeyboardAppearanceAlert, 深灰 石墨色 |
124 | text.delegate = self; |
127 | [self.window addSubview:text]; |
130 | UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@ "right.png" ]]; |
131 | text.rightView=image; |
132 | text.rightViewMode = UITextFieldViewModeAlways; |
135 | UITextFieldViewModeNever, |
136 | UITextFieldViewModeWhileEditing, |
137 | UITextFieldViewModeUnlessEditing, |
138 | UITextFieldViewModeAlways |
139 | } UITextFieldViewMode; |
144 | 类要采用UITextFieldDelegate协议 |
146 | text.delegate = self; 声明text的代理是我,我会去实现把键盘往下收的方法 这个方法在UITextFieldDelegate里所以我们要采用UITextFieldDelegate这个协议 |
148 | - ( BOOL )textFieldShouldReturn:(UITextField *)textField |
150 | [text resignFirstResponder]; |
156 | 除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。 |
160 | – placeholderRectForBounds: |
161 | – drawPlaceholderInRect: |
162 | – borderRectForBounds: |
163 | – editingRectForBounds: |
164 | – clearButtonRectForBounds: |
165 | – leftViewRectForBounds: |
166 | – rightViewRectForBounds: |
170 | - ( BOOL )textFieldShouldBeginEditing:(UITextField *)textField{ |
177 | - ( void )textFieldDidBeginEditing:(UITextField *)textField{ |
181 | - ( BOOL )textFieldShouldEndEditing:(UITextField *)textField{ |
189 | - ( BOOL )textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ |
199 | - ( BOOL )textFieldShouldClear:(UITextField *)textField{ |
207 | -( BOOL )textFieldShouldReturn:(UITextField *)textField{ |
218 | UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件 |
220 | UITextFieldTextDidBeginEditingNotification |
221 | UITextFieldTextDidChangeNotification |
222 | UITextFieldTextDidEndEditingNotification |
223 | 当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。 |
225 | 因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知 |
227 | UIKeyboardWillShowNotification |
228 | UIKeyboardDidShowNotification |
229 | UIKeyboardWillHideNotification |
230 | UIKeyboardDidHideNotification |
233 | 2、Placeholder : 可以在文本框中显示灰色的字,用于提示用户应该在这个文本框输入什么内容。当这个文本框中输入了数据时,用于提示的灰色的字将会自动消失。 |
235 | 4、Disabled : 若选中此项,用户将不能更改文本框内容。 |
237 | 6、Border Style : 选择边界风格。 |
238 | 7、Clear Button : 这是一个下拉菜单,你可以选择清除按钮什么时候出现,所谓清除按钮就是出一个现在文本框右边的小 X ,你可以有以下选择: |
239 | 7.1 Never appears : 从不出现 |
240 | 7.2 Appears while editing : 编辑时出现 |
241 | 7.3 Appears unless editing : |
242 | 7.4 Is always visible : 总是可见 |
243 | 8、Clear when editing begins : 若选中此项,则当开始编辑这个文本框时,文本框中之前的内容会被清除掉。比如,你现在这个文本框 A 中输入了 "What" ,之后去编辑文本框 B,若再回来编辑文本框 A ,则其中的 "What" 会被立即清除。 |
244 | 9、Text Color : 设置文本框中文本的颜色。 |
245 | 10、Font : 设置文本的字体与字号。 |
246 | 11、Min Font Size : 设置文本框可以显示的最小字体(不过我感觉没什么用) |
247 | 12、Adjust To Fit : 指定当文本框尺寸减小时,文本框中的文本是否也要缩小。选择它,可以使得全部文本都可见,即使文本很长。但是这个选项要跟 Min Font Size 配合使用,文本再缩小,也不会小于设定的 Min Font Size 。 |
249 | 13、Captitalization : 设置大写。下拉菜单中有四个选项: |
251 | 13.2 Words : 每个单词首字母大写,这里的单词指的是以空格分开的字符串 |
252 | 13.3 Sentances : 每个句子的第一个字母大写,这里的句子是以句号加空格分开的字符串 |
253 | 13.4 All Characters : 所以字母大写 |
254 | 14、Correction : 检查拼写,默认是 YES 。 |
255 | 15、Keyboard : 选择键盘类型,比如全数字、字母和数字等。 |
257 | 17、Return Key : 选择返回键,可以选择 Search 、 Return 、 Done 等。 |
258 | 18、Auto-enable Return Key : 如选择此项,则只有至少在文本框输入一个字符后键盘的返回键才有效。 |
259 | 19、Secure : 当你的文本框用作密码输入框时,可以选择这个选项,此时,字符显示为星号。 |
262 | 1.Alignment Horizontal 水平对齐方式 |
263 | 2.Alignment Vertical 垂直对齐方式 |
264 | 3.用于返回一个 BOOL 值 输入框是否 Selected(选中) Enabled(可用) Highlighted(高亮) |
268 | ( BOOL )textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ |
270 | cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet]; |
272 | NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@ "" ]; |
274 | BOOL canChange = [string isEqualToString:filtered]; |
279 | 上面那个NUMBERS是一个宏,可以在文件顶部定义: |
280 | #define NUMBERS @”0123456789n” (这个代表可以输入数字和换行,请注意这个n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件,因为你自己限制不让输入n,好惨,我在项目中才发现的。) |
281 | 所以,如果你要限制输入英文和数字的话,就可以把这个定义为: |
282 | #define kAlphaNum @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″。 |
283 | 当然,你还可以在以上方法 return 之前,做一提示的,比如提示用户只能输入数字之类的。如果你觉得有需要的话。 |
287 | - ( BOOL )textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; |
290 | if ([string isEqualToString:@ "n" ]) |
295 | NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; |
297 | if (self.myTextField == textField) |
299 | if ([toBeString length] > 20) { |
300 | textField.text = [toBeString substringToIndex:20]; |
301 | UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@ "超过最大字数不能输入了" delegate:nil cancelButtonTitle:@ "Ok" otherButtonTitles:nil, nil] autorelease]; |
(转自;http://my.oschina.net/u/936286/blog/131010)