uitextfiled 輸入 過濾



// 空格過濾


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSString *str = textField.text;
    
     NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    if ([string isEqualToString:@" "]) {
        
        if (range.location == 0 || [[str substringFromIndex:str.length - 1] isEqualToString:@" "])
            return NO;
    }
    return YES;
}





-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSString *str = textField.text;
    
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    if ([string isEqualToString:@" "]) {
        
        if (range.location == 0 || [[str substringFromIndex:str.length - 1] isEqualToString:@" "])
            return NO;
    }
    
    if (kSingleTFInputVCType_Number == _type) {
        
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:kNumbers] invertedSet];//[itemInfo.type isEqualToString:@"service_price"] ? kNumbersPeriod:kNumbers
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        
        if ([string isEqualToString:filtered])
        {
            
            if (range.location == 0 && [string isEqualToString:@"."]) {
                return NO;
            }else if (range.location == 1 &&([textField.text intValue] == 0) && ![string isEqualToString:@"."]) {
                textField.text = string;
                return NO;
            } else if ([textField.text rangeOfString:@"."].length > 0 ){
                
                if ([string isEqualToString:@"."]) {
                    return NO;
                }else{
                    NSString *subStr = [[textField.text componentsSeparatedByString:@"."] lastObject];
                    
                    if (subStr.length >= 2 && ![string isEqualToString:@""]) {
                        return NO;
                    }else{
                        return YES;
                    }
                    
                    
                }
            }else{
                return YES; //range.location <= 6 ? YES:NO;
            }
            
        }
        else
            return NO;
        
    }else{
        
        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
        
        if ((range.location > _maxInput && ![string isEqualToString:@""]) || str.length > _maxInput) {
            return NO;
        }
        return YES;
    }
    
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章