Вопрос

I have a UITextField in my iOS app where I am already modifying the user's input. I realize now that I need to also make sure that the UITextField can hold a certain number of characters. I am implementing the delegate method as follows:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if([[string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]]
        isEqualToString:@""])
        return YES;

    NSString *previousValue = [[[textField.text stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] stringByReplacingOccurrencesOfString:@"." withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@""];
    string = [string stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
    NSString *modifiedValue = [NSString stringWithFormat:@"%@%@", previousValue, string];

    if ([modifiedValue length] == 1) {

        modifiedValue = [NSString stringWithFormat:@"0.0%@", string];

    }

    else if ([modifiedValue length] == 2) {

        modifiedValue = [NSString stringWithFormat:@"0.%@%@", previousValue, string];

    }

    else if ([modifiedValue length] > 2) {

        modifiedValue = [NSString stringWithFormat:@"%@.%@",[modifiedValue substringToIndex: modifiedValue.length-2],[modifiedValue substringFromIndex:modifiedValue.length-2]];

    }


    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:modifiedValue];
    modifiedValue = [formatter stringFromNumber:decimal];
    [textField setText:modifiedValue];

    return NO;

}

I am not sure with my code above how to factor in the limitation of 22 characters. Can anyone see what it is I need to do?

Это было полезно?

Решение

try like this,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([textField.text length]<=22)
   return YES;
else
   return NO;
}

OR

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

return (textField.text.length <= 22);
}

Другие советы

This is the solution that works best for me, as the answers above won't allow you delete once you've reached the maximum characters.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // allow adding of chars
    if (textField.text.length < MAX_CHARS) {
        return YES;
    }
    // allow deleting
    if (textField.text.length == MAX_CHARS && string.length == 0) {
        return YES;
    }

    return NO;
}

It's not the prettiest solution and could probably be refined to handle additional cases, but this worked well for my needs and thought it may be helpful for someone else.

    if ((string.length == 0) || textField.text.length <= MAX_LIMIT)
        return YES;
    else
        return NO;

This is the solution which i found to be perfect.

Try this:-

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {


        print("chars \(textField.text!.characters.count) \( string)")

        if(textField.text!.characters.count > 20 && range.length == 0) {
            print("Please summarize in 20 characters or less")
            return false;
        }

        return true;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top