在UITextView中防止小写

时间:2014-04-13 07:27:43

标签: ios objective-c uitextview

我试图阻止用户在UITextView中输入任何小写字符。

我创建了一个可接受字符的#define字符集

#define VALID_CHARACTERS @"ABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890"

现在我想知道如何在用户尝试输入与我的VALID_CHARACTERS不匹配的字符时,在UITextView委托中返回no。

3 个答案:

答案 0 :(得分:2)

我建议使用textView:shouldChangeTextInRange:replacementText:,如果替换文字包含无效字符,则返回NO。

编辑(回复您的评论):

// Goal: Remove all the non-valid characters from the replacement string
// then see if the string is the same as the original replacement string;
// if it is, then the string is valid and return YES, else return NO

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

    // First step - define a character set with the valid characters
    NSMutableCharacterSet *validSet = [NSMutableCharacterSet characterSetWithCharactersInString:VALID_CHARACTERS];

    // Second step - define a character set with the inverse, i.e invalid characters
    NSCharacterSet *invalidSet = [validSet invertedSet];

    // Then remove all invalid characters by separating the string into components
    // separated by the invalid characters using componentsSeparatedByCharactersInSet:
    // and then rejoining the set using componentsJoinedByString: so that it now only
    // contains valid characters
    NSString *filteredString = [[string componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@""];

    // Compare that filtered string with the original string then see if its
    // the same as the replacement string; if the same (i.e. no invalid characters
    // have been removed), return yes, if not, return no.
    return ([filteredString isEqualToString:string]);

}

答案 1 :(得分:2)

要限制文本视图的可能输入字符,请实现文本视图 代表这样:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{    
    static NSString *validChars = @"ABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890\n";
    NSCharacterSet *validSet = [NSCharacterSet characterSetWithCharactersInString:validChars];
    if ([[text stringByTrimmingCharactersInSet:validSet] length] > 0)
        return NO;
    return YES;
}
\n中的

validChars用于RETURN键(您可能或不想允许)。

根据评论中的建议,您可以将小写字母转换为大写字母 自动:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{    
    text = [text uppercaseString];
    static NSString *validChars = @"ABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890\n";
    NSCharacterSet *validSet = [NSCharacterSet characterSetWithCharactersInString:validChars];
    if ([[text stringByTrimmingCharactersInSet:validSet] length] > 0)
        return NO;
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:text];
    return NO;
}

要允许其他语言的大写字母和数字,请更改 validSet

的定义
NSMutableCharacterSet *validSet = [NSMutableCharacterSet uppercaseLetterCharacterSet];
[validSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
[validSet formUnionWithCharacterSet:[NSCharacterSet newlineCharacterSet]];

由于经常调用文本视图委托,您可以通过以下方式进行改进 使用GCD只计算一次有效字符集:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    static NSCharacterSet *validSet;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSMutableCharacterSet *tmpSet = [NSMutableCharacterSet uppercaseLetterCharacterSet];
        [tmpSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
        [tmpSet formUnionWithCharacterSet:[NSCharacterSet newlineCharacterSet]];
        validSet = [tmpSet copy];
    });
    text = [text uppercaseString];
    if ([[text stringByTrimmingCharactersInSet:validSet] length] > 0)
        return NO;
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:text];
    return NO;
}

答案 2 :(得分:0)

最简单的解决方案:

使用UITextView委托方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

方法实施:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]].location != NSNotFound) {
        textView.text = [textView.text stringByReplacingCharactersInRange:range withString:text.uppercaseString];
        return NO;
    }

    return YES;
}