2

我在输入 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;
}

谢谢。

4

3 回答 3

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;
}
于 2014-08-18T17:09:03.840 回答
1

我同意评论,即这不是可取的设计。

不过,编程问题很有趣。我的方法是首先“规范化”文本,即删除所有非数字字符,然后应用您的逻辑。

textField.text=[[textField.text componentsSeparatedByCharactersInSet:
     [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
     componentsJoinedByString:@""];
于 2013-10-22T21:00:36.430 回答
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;
}
于 2014-02-10T10:42:09.493 回答