Is there an easier way to textfield phone?

Asked

Viewed 564 times

1

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

int length = [self getLength:textField.text];
//NSLog(@"Length  =  %d ",length);

if(length == 11)
{
    if(range.length == 0)
        return NO;
}

if(length == 2)
{
    NSString *num = [self formatNumber:textField.text];
    textField.text = [NSString stringWithFormat:@"(%@)",num];
    if(range.length > 0)
        textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
}
else if(length == 7)
{
    NSString *num = [self formatNumber:textField.text];
    //NSLog(@"%@",[num  substringToIndex:3]);
    //NSLog(@"%@",[num substringFromIndex:3]);
    textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:2],[num substringFromIndex:2]];
    if(range.length > 0)
        textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:2],[num substringFromIndex:2]];
}

return YES;
}

-(NSString*)formatNumber:(NSString*)mobileNumber
{

mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

NSLog(@"%@", mobileNumber);

int length = [mobileNumber length];
if(length > 10)
{
    mobileNumber = [mobileNumber substringFromIndex: length-10];
    NSLog(@"%@", mobileNumber);

}


return mobileNumber;
}


-(int)getLength:(NSString*)mobileNumber
{

mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

int length = [mobileNumber length];

return length;


}
  • 5

    Please explain your problem with the current code better. Only the title and a copy/code copy make the question extremely poor. Check out the guides [Ask] and [about]. And welcome to SOPT :)

1 answer

3


I have something generic, which also works with phone, using mask. So, I have a subclass of UITextField who I called CustomTextField. My file .h is more or less like this:

@interface CustomTextField : UITextField

@property (nonatomic, retain) NSString *mask;

- (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

@end

And in my implementation file .m, thus:

NSString *filteredFromStringWithFilter(NSString *string, NSString *filter) {
    NSUInteger onOriginal = 0, onFilter = 0, onOutput = 0;
    char outputString[([filter length])];
    BOOL done = NO;

    while(onFilter < [filter length] && !done) {
        char filterChar = [filter characterAtIndex:onFilter];
        char originalChar = onOriginal >= string.length ? '\0' : [string characterAtIndex:onOriginal];
        switch (filterChar) {
            case '#':
                if (originalChar=='\0') {
                    done = YES;
                    break;
                }
                if(isdigit(originalChar)) {
                    outputString[onOutput] = originalChar;
                    onOriginal++;
                    onFilter++;
                    onOutput++;
                } else {
                    onOriginal++;
                }
                break;
            default:
                outputString[onOutput] = filterChar;
                onOutput++;
                onFilter++;
                if (originalChar == filterChar)
                    onOriginal++;
                break;
        }
    }
    outputString[onOutput] = '\0'; // Cap the output string

    return [NSString stringWithUTF8String:outputString];
}

- (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (self.mask) {
        NSString *changedString = [self.text stringByReplacingCharactersInRange:range withString:string];

        if (range.length == 1 && // Only do for single deletes
            string.length < range.length &&
            [[self.text substringWithRange:range] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]].location == NSNotFound) {
            NSInteger location = changedString.length-1;
            if (location > 0) {
                for (; location > 0; location--) {
                    if (isdigit([changedString characterAtIndex:location])) {
                        break;
                    }
                }

                changedString = [changedString substringToIndex:location];
            }
        }

        [self setText:filteredFromStringWithFilter(changedString, self.mask)];

        return NO;
    }

    return YES;
}

And in my ViewController:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
    return [(CustomTextField *)textField shouldChangeCharactersInRange:range replacementString:string];
}

Ready, so generic to use mask as you want, for example using the interface:

inserir a descrição da imagem aqui

  • Congratulations on the solution!

  • 1

    But to run, with me I needed to add a delegate from the field to the Viewcontroller - at the storyboard inspector click on Ctrl on the element and drag to the Viewcontroller (icon Amarelinho) and choose "delegate" and funfou gorgeous!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.