Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a string automatically while user editing UITEXTFIELD

I want to my uitextfield be like XXX.XXX.XXX/XX at the end of typing.

To limit the lenght I use this:

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

        NSUInteger newLength = [textField.text length] + [string length] - range.length;
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));
    } else{
        return YES;
    }

}

The problem is how to insert the "." and "/" while user still editing it.

like image 713
Marckaraujo Avatar asked Dec 04 '22 08:12

Marckaraujo


1 Answers

The following code should do the following:

  1. Limit the number of characters that can be typed/pasted into the text field
  2. Automatically add periods and slashes at the appropriate locations
  3. Prevent issues from the user copy/pasting a string that already has the necessary periods/slashes

That said, there is almost certainly more efficient ways to do this; but if you're not concerned about code length it'll do the trick just fine.

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = textField.text;

    // If we're trying to add more than the max amount of characters, don't allow it
    if ([text length] == 14 && range.location > 13) {
          return NO; 
    }

    // First lets add the whole string we're going for
    text = [text stringByReplacingCharactersInRange:range withString:string];

    // Now remove spaces, periods, and slashes (since we'll add these automatically in a minute)
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"." withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"/" withString:@""];

    // We need to use an NSMutableString to do insertString calls in a moment
    NSMutableString *mutableText = [text mutableCopy];

    // Every 4th char will be a '.', but we don't want to check more than the first 8 characters
    for (NSUInteger i = 3; i < mutableText.length && i < 8; i += 4) {
        [mutableText insertString:@"." atIndex:i];
    }
    // If the text is more than 11 characters, we also want to insert a '/' at the 11th character index
    if (mutableText.length > 11) {
        [mutableText insertString:@"/" atIndex:11];
    }

    // lets set text to our new string
    text = mutableText;

    // Now, lets check if we need to cut off extra characters (like if the person pasted a too-long string)
    if (text.length > 14) {
        text = [text stringByReplacingCharactersInRange:NSMakeRange(14, mutableText.length-14) withString:@""];
    }

    // Finally, set the textfield to our newly modified string!
    textField.text = text;

    return NO;
}
like image 62
Doc Avatar answered May 16 '23 12:05

Doc