美文网首页已解决
UITextField输入浮点数

UITextField输入浮点数

作者: 虫yu | 来源:发表于2020-05-21 15:26 被阅读0次
//只能输入数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
         textField.keyboardType=UIKeyboardTypeNumberPad;//只有数字的键盘
         return [self validateNumber:string] ;
}

//设置只能输入数字
-(BOOL)validateNumber:(NSString * )number{
    
    BOOL res = YES;
    NSScanner      *scanner    = [NSScanner scannerWithString:number];
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    NSString *buffer;
    if ( ![scanner scanCharactersFromSet:tmpSet intoString:&buffer] && ([number length] != 0) )
    {
        [self alertViewmessage:@"只允许输入数字"];
    }
 
    return res;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    textField.keyboardType=UIKeyboardTypeDecimalPad;//有数字和小数点的键盘
    
    NSScanner      *scanner    = [NSScanner scannerWithString:string];
    NSCharacterSet *numbers;
    NSRange         pointRange = [textField.text rangeOfString:@"."];
    
    if ( (pointRange.length > 0) && (pointRange.location < range.location  || pointRange.location > range.location + range.length) )
    {
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    } else {
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    }
    
    if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] )
    {
        return NO;
    }
    
    short remain = 2; //默认保留2位小数
    
    NSString *tempStr = [textField.text stringByAppendingString:string];
    NSUInteger strlen = [tempStr length];
    if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
        if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。
            //                        [self alertViewmessage:@"您已输入过“.”"];
            return NO;
        }
        if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。
            //                        [self alertViewmessage:@"小数点后只允许输入2位数字"];
            return NO;
        }
    }else {
        // 没有输入小数点之前,也就是整数部分,只能输入两位
        if (strlen > 2 && ![string isEqualToString:@"."]) {
            return NO;
        }
    }
    
    NSRange zeroRange = [textField.text rangeOfString:@"0"];
    if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
        if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。
            textField.text = string;
            return NO;
        }else{
            if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。
                if([string isEqualToString:@"0"]){
                    return NO;
                }
            }
        }
    }
    
    NSString *buffer;
    if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) )
    {
        //                    [self alertViewmessage:@"只允许输入数字和“.”"];
        return NO;
    }
    return YES;
}

转载自

UITextField只能输入数字以及输入数字和小数点并且小数点后保留2位小数

相关文章

网友评论

    本文标题:UITextField输入浮点数

    本文链接:https://www.haomeiwen.com/subject/hemgohtx.html