2012-09-04 50 views
12

我有兩個文本字段,我想限制字符的數量和類型。我已經使用了下面幾段代碼分別完成每個功能,但是無法在同一個功能中找到兩種方法。UITextField限制字符的數量和類型

要限制字符的類型:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    // Only characters in the NSCharacterSet you choose will insertable. 
    NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet]; 
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""]; 
    return [string isEqualToString:filtered]; 
} 

並限制字符數:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
if (textField.text.length >= 10 && range.length == 0) 
return NO; 
return YES; 
} 

回答

20
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if (textField.text.length >= 10 && range.length == 0) 
     return NO; 
    // Only characters in the NSCharacterSet you choose will insertable. 
    NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet]; 
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""]; 
    return [string isEqualToString:filtered]; 
} 

編輯

如果你想爲第三個文本字段添加不同的條件,那麼你可以這樣做。
創建3文本參考FILD說thirdField

然後用這個

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if (textField == thirdField) { 
     //your contion e.g 
     if (textField.text.length < 7) { 
      return YES; 
     } else { 
      return NO; 
     }   
    } 
    else { 
     if (textField.text.length >= 10 && range.length == 0) 
      return NO; 
     // Only characters in the NSCharacterSet you choose will insertable. 
     NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet]; 
     NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""]; 
     return [string isEqualToString:filtered]; 
    } 
} 
+0

如果我添加了第三個文本字段,但想要對其應用不同的條件,我該怎麼做? – Tool

+0

我編輯了我的答案。 –

2

這是這樣:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
    { 
     // Only characters in the NSCharacterSet you choose will insertable. 
     NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet]; 
     NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""]; 
     bool cond1 = [string isEqualToString:filtered]; 

    if (textField.text.length >= 10 && range.length == 0){ 
      return NO; 
    }else{ 
      return (cond1); 
    } 

    } 
+0

我很快寫道,但主意是檢查兩個條件都OK – Resh32

+0

'if(cond1)return YES;否則返回NO' - 爲什麼不只是'return(cond1)'?它總是讓我困惑。 – Cyrille

3

這裏是最乾淨的方法來限制在爲UITextField輸入的字符中的一個。這種方法允許使用多個預定義的NSCharacterSets。

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

    NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet]; 
    [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]]; 
    [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]]; 
    [allowedCharacters addCharactersInString:@":./"]; //allow arbitrary characters 

    if([string rangeOfCharacterFromSet:allowedCharacters.invertedSet].location == NSNotFound){ 

     return YES; 

    } 

    return NO; 

}