2013-10-22 94 views
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; 
} 

謝謝。

+2

是您的應用程序將成爲美國和加拿大以外提供?如果是這樣,你需要處理更多的電話格式。 – rmaddy

+0

從用戶體驗角度來看,我建議在用戶輸入完成後設置電話格式(即鍵盤退出時)。另外,如果可能,請在'placeholder'文本中補充此格式。 – Ashok

回答

1

我同意評論意見,即這不是可取的設計。

不過,編程問題很有意思。我的做法是首先「正常化」文本,即刪除所有非數字字符,然後應用您的邏輯。

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

這樣做。按我的預期工作。非常感謝您的關注和建議。 – Alex

0

看一看這段代碼,這可能有助於UA位

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; 
} 
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; 
}