键入时设置电话号码格式

时间:2013-10-22 20:36:28

标签: ios iphone objective-c

我在输入textField时设置电话号码格式,如(222)233-3441。当用户继续输入超过14个字符(包括特殊字符)时,将删除所有特殊字符并单独显示数字(即222233344188)。当他们在删除某些字符时返回14个字符时,将再次设置电话号码格式。我实现了我想要的。但在删除时遇到问题。

  • 删除时 - 当字符数达到10时应用格式。所以2222333441变为(222)233-3441。
  • 继续删除,获取循环,因为格式被应用并且角色再次达到10。

成为空白继续前进。请提出宝贵的建议以解决此问题。

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


    if(string.length!=0){   //detect backspace

        if (textField.text.length == 0)
            textField.text = [NSString stringWithFormat:@"(%@",textField.text];

        if (textField.text.length == 4)
            textField.text = [NSString stringWithFormat:@"%@) ",textField.text];

        if (textField.text.length == 9)
            textField.text = [NSString stringWithFormat:@"%@-",textField.text];

        if (textField.text.length>13){

            NSString *value=[NSString stringWithString:textField.text];
            textField.text=[[value componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]componentsJoinedByString:@""];

        }
    }
    else{

        if(textField.text.length==11){

            NSMutableString *text=[NSMutableString stringWithString:textField.text];
            [text insertString:@"(" atIndex:0];
            [text insertString:@") " atIndex:4];
            [text insertString:@"-" atIndex:9];
            textField.text=text;
        }

     }

     return YES;
}

感谢。

3 个答案:

答案 0 :(得分:4)

我建议更紧凑的解决方案:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    BOOL result = YES;
    if (string.length != 0) {
        NSMutableString *text = [NSMutableString stringWithString:[[textField.text componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]];
        [text insertString:@"(" atIndex:0];

        if (text.length > 3)
            [text insertString:@") " atIndex:4];

        if (text.length > 8)
            [text insertString:@"-" atIndex:9];

        if (text.length > 13) {
            text = [NSMutableString stringWithString:[text substringToIndex:14]];
            result = NO;
        }
        textField.text = text;
    }

    return result;
}

答案 1 :(得分:1)

我同意这些意见,即这不是可取的设计。

然而,编程问题很有趣。我的方法是首先“标准化”文本,即删除所有非数字字符,然后应用你的逻辑。

textField.text=[[textField.text componentsSeparatedByCharactersInSet:
     [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
     componentsJoinedByString:@""];

答案 2 :(得分:0)

看看这段代码,这可能对你有所帮助

txtlpmobile.text是字符串(手机不输入)

 int length = [self getLength:txtLpMobile.text];
            if(length == 10) {
                if(range.length == 0)
                    return NO;
            }
            if(length == 3){
                NSString *num = [self formatNumber:txtLpMobile.text];
                txtLpMobile.text = [NSString stringWithFormat:@"(%@) ",num];

                if(range.length > 0) {
                    txtLpMobile.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];

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

            NSUInteger newLength;
            newLength = [txtLpMobile.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));

格式化数字

-(NSString*)formatNumber:(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];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
    }
    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;
}