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 は文字列です (Mobile no ur going enter)

 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 に答える