iOS textField只能输入数字以及2位小数

只需实现下面的代理方法即可,其中100000为输入最大值,可自行更改。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    BOOL isHaveDian = YES;
    if ([textField.text rangeOfString:@"."].location == NSNotFound) {
        isHaveDian = NO;
    }
    if (string.length == 0) {
        return YES;
    }
    unichar single = [string characterAtIndex:0];//当前输入的字符
    if ((single >= '0' && single <= '9') || single == '.') {
        if (range.location == 0) {//光标在字符串开头
            if (single == '.' || single == '0') {
                [MBProgressHUD showError:@"亲,价格不能以0和小数点开头哦"];
                return NO;
            }
            else {
                NSMutableString *temp = [textField.text mutableCopy];
                [temp insertString:string atIndex:range.location];
                
                if (temp.floatValue > 100000) {
                    [MBProgressHUD showError:@"最大金额为100000"];
                    return NO;
                }
                return YES;
            }
        }
        else {
           if (single == '.') {
               if (isHaveDian) {
                   return NO;
               }
               else {
                   NSMutableString *temp = [textField.text mutableCopy];
                   [temp insertString:string atIndex:range.location];
                   
                   if (temp.floatValue < 100000) {
                       return YES;
                   }
                   else {
                       [MBProgressHUD showError:@"最大金额为100000"];
                       return NO;
                   }
               }
           }
           else {
               if (isHaveDian) {
                   NSRange dotRange = [textField.text rangeOfString:@"."];
                   if (range.location > dotRange.location) {
                       if (textField.text.length - dotRange.location <= 2) {
                           return YES;
                       }
                       else {
                           return NO;
                       }
                   }
                   else {
                       NSMutableString *temp = [textField.text mutableCopy];
                       [temp insertString:string atIndex:range.location];
                       
                       if (temp.floatValue > 100000) {
                           [MBProgressHUD showError:@"最大金额为100000"];
                           return NO;
                       }
                       return YES;
                   }
               }
               else {
                   NSMutableString *temp = [textField.text mutableCopy];
                   [temp insertString:string atIndex:range.location];
                   
                   if (temp.floatValue > 100000) {
                       [MBProgressHUD showError:@"最大金额为100000"];
                       return NO;
                   }
                   return YES;
               }
           }
        }
    }
    else {
        [MBProgressHUD showError:@"亲,您输入的格式不正确"];
        return NO;
    }
}

你可能感兴趣的:(iOS textField只能输入数字以及2位小数)