1

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?

syedfa
  • 2,801
  • 1
  • 41
  • 74
  • you want enter the data upto 22 characters in textfield right? – Balu Mar 29 '13 at 09:59
  • possible duplicate of [Set the maximum character length of a UITextField](http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield) – Larme Apr 20 '14 at 14:49

4 Answers4

4

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);
}
Balu
  • 8,470
  • 2
  • 24
  • 41
  • 1
    Needlessly verbose. Why not just `return (textField.text.length <= 22);` – borrrden Mar 29 '13 at 10:02
  • As `if ([text length] <= 22)` already returns a bool, why not `return [text length] <= 22;` – n_b Mar 29 '13 at 10:03
  • Won't that allow 23 characters, if you return Yes when the length is 22? Also, won't this prevent the user from deleting characters once they've hit 23 characters? – stone Feb 10 '15 at 06:04
  • This code won't work if you reach the limit and try to delete some letter.. this solution works: http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield – Chanchal Raj Jan 29 '17 at 18:23
2

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.

iksnae
  • 1,030
  • 11
  • 18
0
    if ((string.length == 0) || textField.text.length <= MAX_LIMIT)
        return YES;
    else
        return NO;

This is the solution which i found to be perfect.

Priyansh
  • 1,163
  • 11
  • 28
0

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;
}
Abhijeet Mallick
  • 1,740
  • 2
  • 16
  • 21