views:

660

answers:

4

Hello everyone, I was wondering how I can format the textField that I'm using for a phone number (ie like the "Add New Contact" page on the iPhone. When I enter in a new mobile phone, ex. 1236890987 it formats it as (123) 689-0987.) I already have the keyboard set as the number pad.

Thanks!

A: 

Unfortunately, you have to do it yourself. The contact app uses undocumented APIs. For some reason attaching input formatters to text fields is not exposed on the iPhone the way it is on the Mac. Feel free to file a feature enhancement bug report.

Ramin
A: 

u have to do to this manually . take a notification of textField and check length of field text and format it according to country. if any problem let me know. I have done this

sandy
Hey goeff,it would be awesome if you could show me the code to do this because I just started coding in objective-c and it's very confusing to me.Thanks in advance!!
A: 

I too would love to see how someone did this and some sample code. I started coding this using a delegate to the UITextField that monitors changes and reviews the phone# string and attempts to format it. Also using the shouldChangeTextInRange: method. But was hoping someone would have developed something by now that can be packaged and implemented easily.

Wally
A: 

I've been struggling with this for a couple of hours, here's what I have:

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


NSUInteger currentLength = textField.text.length;


NSCharacterSet *numbers = [NSCharacterSet decimalDigitCharacterSet];


if (range.length == 1) {
    return YES;
}


if ([numbers characterIsMember:[string characterAtIndex:0]]) {


    if ( currentLength == 3 ) 
    {

        if (range.length != 1) 
        {

            NSString *firstThreeDigits = [textField.text substringWithRange:NSMakeRange(0, 3)];

            NSString *updatedText;

            if ([string isEqualToString:@"-"]) 
            {
                updatedText = [NSString stringWithFormat:@"%@",firstThreeDigits];
            }

            else 
            {
                updatedText = [NSString stringWithFormat:@"%@-",firstThreeDigits];
            }

            [textField setText:updatedText];
        }           
    }

    else if ( currentLength > 3 && currentLength < 8 ) 
    {

        if ( range.length != 1 ) 
        {

            NSString *firstThree = [textField.text substringWithRange:NSMakeRange(0, 3)];
            NSString *dash = [textField.text substringWithRange:NSMakeRange(3, 1)];

            NSUInteger newLenght = range.location - 4;

            NSString *nextDigits = [textField.text substringWithRange:NSMakeRange(4, newLenght)];

            NSString *updatedText = [NSString stringWithFormat:@"%@%@%@",firstThree,dash,nextDigits];

            [textField setText:updatedText];

        }

    }

    else if ( currentLength == 8 ) 
    {

        if ( range.length != 1 ) 
        {
            NSString *areaCode = [textField.text substringWithRange:NSMakeRange(0, 3)];

            NSString *firstThree = [textField.text substringWithRange:NSMakeRange(4, 3)];

            NSString *nextDigit = [textField.text substringWithRange:NSMakeRange(7, 1)];

            [textField setText:[NSString stringWithFormat:@"(%@) %@-%@",areaCode,firstThree,nextDigit]];
        }

    }
}

else {
    return NO;
}

return YES;

}

I hope someone can contribute.

Romeo