输入带小数点的键盘(小数点为2位为例)

  1. textField.keyboardType = UIKeyboardTypeDecimalPad;  
  2. // A number pad with a decimal point.苹果给的注释就是一个带小数点的键盘  
  3.   
  4. #pragma mark- UITextFieldDelegate  
  5. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
  6. {  
  7.     if (textField.keyboardType == UIKeyboardTypeDecimalPad) {  
  8.         if ([textField.text rangeOfString:@"."].location==NSNotFound) {  
  9.             isHaveDian=NO;  
  10.         }  
  11.         if ([string length]>0)  
  12.         {  
  13.             unichar single=[string characterAtIndex:0];//当前输入的字符  
  14.             if ((single >='0' && single<='9') || single=='.')//数据格式正确  
  15.             {  
  16.                 //首字母不能为0和小数点  
  17.                 if([textField.text length]==0){  
  18.                     if(single == '.'){  
  19.                         //                    [self alertView:@"亲,第一个数字不能为小数点"];  
  20.                         [textField.text stringByReplacingCharactersInRange:range withString:@""];  
  21.                         return NO;  
  22.                           
  23.                     }  
  24.                 }else if (textField.text.length == 1 && [textField.text isEqualToString:@"0"]){  
  25.                     if (single != '.') {  
  26.                         //                    [self alertView:@"亲,第一个数字不能为0"];  
  27.                         [textField.text stringByReplacingCharactersInRange:range withString:@""];  
  28.                         return NO;  
  29.                           
  30.                     }  
  31.                 }  
  32.                 if (single=='.')  
  33.                 {  
  34.                     if(!isHaveDian)//text中还没有小数点  
  35.                     {  
  36.                         isHaveDian=YES;  
  37.                         return YES;  
  38.                     }else  
  39.                     {  
  40.                         //                    [self alertView:@"亲,您已经输入过小数点了"];  
  41.                         [textField.text stringByReplacingCharactersInRange:range withString:@""];  
  42.                         return NO;  
  43.                     }  
  44.                 }  
  45.                 else  
  46.                 {  
  47.                     if (isHaveDian)//存在小数点  
  48.                     {  
  49.                         //判断小数点的位数  
  50.                         NSRange ran=[textField.text rangeOfString:@"."];  
  51.                         NSInteger tt=range.location-ran.location;  
  52.                         if (tt <= 2){  
  53.                             return YES;  
  54.                         }else{  
  55.                             //                        [self alertView:@"亲,您最多输入两位小数"];  
  56.                             return NO;  
  57.                         }  
  58.                     }  
  59.                     else  
  60.                     {  
  61.                         return YES;  
  62.                     }  
  63.                 }  
  64.             }else{//输入的数据格式不正确  
  65.                 //            [self alertView:@"亲,您输入的格式不正确"];  
  66.                 [textField.text stringByReplacingCharactersInRange:range withString:@""];  
  67.                 return NO;  
  68.             }  
  69.         }  
  70.         else  
  71.         {  
  72.             return YES;  
  73.         }  
  74.     }  
  75.     return YES;  
  76. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值