2012-05-15 112 views

回答

1

有有點一招這個的,你需要計算新的字符串是什麼之前,你可以測試是否允許或拒絕的變化

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; 

    if ([newString length] > 15) { 
     return FALSE; 
    } else { 
     return TRUE; 
    } 
} 
0

您可以檢查是否輸入和計數字符

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range  replacementString:(NSString *)string 
    { 
    if (range.length > 15) { 
     // delete 
    } 
    else 
    { 
     // add 
    } 
} 
-1
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if (textField.text.length >= 15) 
    { 
     return NO; //return NO to not change text 
    } 

return YES; 

} 
+0

它不會允許的字符刪除一次15個字符已經達到。 –

0

使用方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if (([textField.text length] - range.length) == 15) { 
     return NO; 
    } 
    return YES; 
} 

希望它幫助。快樂編碼:)

1

//它的大碼但工作對我罰款

//把你的文本字段名稱,而不是YourTextFieldName在這段代碼

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

    { 

     if (textField==YourTextFieldName) 
     { 

     NSString *text = nil; 
     int MAX_LENGTH = 12; 

     text = YourTextFieldName.text; 
     if ([text length] <= 7) 
     { 
      NSString *separator = @"-"; 
      int seperatorInterval = 3; 
      NSString *originalString = [textField.text   stringByReplacingOccurrencesOfString:separator withString:@""]; 

      if (![originalString isEqualToString:@""] && ![string isEqualToString:@""]) 
      { 

       NSString *lastChar = [YourTextFieldName.text substringFromIndex:[YourTextFileName.text length] - 1]; 
       int modulus = [originalString length] % seperatorInterval; 

       if (![lastChar isEqualToString:separator] && modulus == 0) 
       { 
        YourTextFieldName.text = [YourTextFieldName.text stringByAppendingString:separator]; 
       } 
      } 
     } 
     if ([text length] > 7) 
     { 
      NSString *separator = @"-"; 
      int seperatorInterval = 6; 
      NSString *originalString = [textField.text stringByReplacingOccurrencesOfString:separator withString:@""]; 

      if (![originalString isEqualToString:@""] && ![string isEqualToString:@""]) 
      { 

       NSString *lastChar = [YourTextFieldName.text substringFromIndex:[YourTextFieldName.text length] - 1]; 
       int modulus = [originalString length] % seperatorInterval; 

       if (![lastChar isEqualToString:separator] && modulus == 0) 
       { 

        YourTextFieldName.text = [YourTextFieldName.text stringByAppendingString:separator]; 
       } 
      } 
     } 

      NSUInteger newLength = [textField.text length] + [string length] - range.length; 
     return (newLength > MAX_LENGTH) ? NO : YES; 
    } 
    return YES; 



    } 
相關問題