UITextField 的用法详解

来源:互联网 发布:象过河软件怎么样 编辑:程序博客网 时间:2024/04/30 04:58

定义一个UITextField 及其基本的属性:

[cpp] view plaincopy
  1.    UITextField _telNum = [[UITextField alloc] init];  
  2.    _telNum.clearsOnBeginEditing = NO;//在输入时不清除原来输入的文字  
  3.    _telNum.delegate = self;//  
  4.    [_telNum addTarget:self action:@selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];//  
  5. _telNum.font = [UIFont boldSystemFontOfSize:14];//  
  6. _telNum.textColor = [UIColor blackColor];//  
  7. _telNum.backgroundColor = [UIColor clearColor];//  
  8. _telNum.borderStyle = UITextBorderStyleNone;  
  9. _telNum.clearButtonMode = UITextFieldViewModeWhileEditing;  
  10. _telNum.autocorrectionType = UITextAutocorrectionTypeNo;  
  11. _telNum.autocapitalizationType  = UITextAutocapitalizationTypeNone;  
  12. _telNum.returnKeyType = UIReturnKeyDefault;  
  13. _telNum.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
  14. _telNum.placeholder = @"默认的文本";  
  15.    _telNum.keyboardType = UIKeyboardTypeNumberPad;//文本输入时,按键弹出的按键为数字  

下面为UITextField 的一些代理方法:

1.开始输入文本时,一般需要判断键盘是否遮挡住了UITextField 视图,是的话需要调整整个视图的位置。

[cpp] view plaincopy
  1. -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {  
  2.       
  3.     CGRect textFrame =  textField.frame;  
  4.     float textY = textFrame.origin.y+textFrame.size.height;  
  5.     float bottomY = self.view.frame.size.height-textY;  
  6.     if(bottomY<216)  //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度  
  7.     {  
  8.         float moveY = 216-bottomY;  
  9.         prewMoveY = moveY;  
  10.       
  11.         NSTimeInterval animationDuration = 0.30f;  
  12.         CGRect frame = self.view.frame;  
  13.         frame.origin.y -=moveY;//view的Y轴上移  
  14.         frame.size.height +=moveY; //View的高度增加  
  15.         self.view.frame = frame;  
  16.         [UIView beginAnimations:@"ResizeView" context:nil];  
  17.         [UIView setAnimationDuration:animationDuration];  
  18.         self.view.frame = frame;  
  19.         [UIView commitAnimations];//设置调整界面的动画效果  
  20.     }  
  21.     return YES;  
  22. }  
2. 输入文本结束时,调回输入前的视图布局

[cpp] view plaincopy
  1. -(void ) textFieldDoneEditing:(id) sender{  
  2.     if (sender == _telNum) {  
  3.         _payButton.enabled = YES;  
  4.           
  5.         NSTimeInterval animationDuration = 0.30f;  
  6.         CGRect frame = self.view.frame;  
  7.         //还原界面  
  8.         frame.origin.y +=prewMoveY;  
  9.         frame.size. height -=prewMoveY;  
  10.         self.view.frame = frame;  
  11.         //self.view移回原位置  
  12.         [UIView beginAnimations:@"ResizeView" context:nil];  
  13.         [UIView setAnimationDuration:animationDuration];  
  14.         self.view.frame = frame;  
  15.         [UIView commitAnimations];  
  16.         [sender resignFirstResponder];  
  17.           
  18.     }  
  19. }  

3.决定文本输入的字数,超过一定的范围则弹出提示框:

[cpp] view plaincopy
  1. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
  2. {  
  3.     if (range.location>=11)  
  4.     {  
  5.         alertMessage(nil, @"超过了电话号码的长度!");  
  6.         return  NO;  
  7.     }  
  8.     else  
  9.     {  
  10.         return YES;  
  11.     }  
  12. }  

4.输入完毕,需要判定输入的文本是否满足要求,可以采用正则表达式来进行判定。例如,我们需要判定输入的数字是否为电话号码,如下代码即行:

[cpp] view plaincopy
  1. // 正则判断手机号码地址格式  
  2. - (BOOL)isMobileNumber:(NSString *)mobileNum  
  3. {  
  4.     /** 
  5.      * 手机号码 
  6.      * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 
  7.      * 联通:130,131,132,152,155,156,185,186 
  8.      * 电信:133,1349,153,180,189 
  9.      */  
  10.     NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";  
  11.     /** 
  12.      10         * 中国移动:China Mobile 
  13.      11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 
  14.      12         */  
  15.     NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";  
  16.     /** 
  17.      15         * 中国联通:China Unicom 
  18.      16         * 130,131,132,152,155,156,185,186 
  19.      17         */  
  20.     NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";  
  21.     /** 
  22.      20         * 中国电信:China Telecom 
  23.      21         * 133,1349,153,180,189 
  24.      22         */  
  25.     NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";  
  26.     /** 
  27.      25         * 大陆地区固话及小灵通 
  28.      26         * 区号:010,020,021,022,023,024,025,027,028,029 
  29.      27         * 号码:七位或八位 
  30.      28         */  
  31.     // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";  
  32.       
  33.     NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];  
  34.     NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];  
  35.     NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];  
  36.     NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];  
  37.       
  38.     if (([regextestmobile evaluateWithObject:mobileNum] == YES)  
  39.         || ([regextestcm evaluateWithObject:mobileNum] == YES)  
  40.         || ([regextestct evaluateWithObject:mobileNum] == YES)  
  41.         || ([regextestcu evaluateWithObject:mobileNum] == YES))  
  42.     {  
  43.         return YES;  
  44.     }  
  45.     else  
  46.     {  
  47.         return NO;  
  48.     }  
  49. }  

5.我们需要在点击非键盘区域实现结束输入的功能,同时键盘视图退出。可以使用如下的代码:

[cpp] view plaincopy
  1. UIControl *controlView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 416.0f)];  
  2. [controlView addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];  
  3. [self.view addSubview:controlView];  
在上面的基础上增加点击消息处理方法:

[cpp] view plaincopy
  1. -(void) backgroundTap:(id) sender{  
  2.     //还原界面  
  3.     NSTimeInterval animationDuration = 0.30f;  
  4.     CGRect frame = self.view.frame;  
  5.     frame.origin.y +=prewMoveY;  
  6.     frame.size. height -=prewMoveY;  
  7.     self.view.frame = frame;  
  8.     //self.view移回原位置  
  9.     [UIView beginAnimations:@"ResizeView" context:nil];  
  10.     [UIView setAnimationDuration:animationDuration];  
  11.     self.view.frame = frame;  
  12.     [UIView commitAnimations];  
  13.     [_telNum resignFirstResponder];  
  14.       
  15.     if(_telNum.text.length>0) {  
  16.         if(![self isMobileNumber:_telNum.text]) {  
  17.             alertMessage(nil, @"电话号码错误!");  
  18.         }  
  19.         else {  
  20.             _payButton.enabled = YES;  
  21.             //_payButton.hidden = NO;  
  22.         }  
  23.     }<span style="color:#ffffff;">  
  24. </span>}  
0 0
原创粉丝点击