【问题标题】:Prevent lower case in UITextView在 UITextView 中防止小写
【发布时间】:2014-05-27 05:27:08
【问题描述】:

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

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

#define VALID_CHARACTERS @"ABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890"

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

【问题讨论】:

  • 不是阻止用户输入 lower,而是始终转换为 upper 提供更好的用户体验。 stackoverflow.com/a/2027227/366346
  • 在下面的答案中“即使将 autocapitalizationType 设置为 UITextAutocapitalizationTypeAllCharacters,用户仍然可以按下大写字母来释放大写锁定”
  • 你的问题是关于 UITextField 还是 UITextView (你都提到了)?你想阻止小写字母还是只允许大写字母和数字?符号、空格、表情符号和其他东西呢?
  • 哎呀,修复了.. UITextView.. 对不起,我已经习惯了 UITextField 的习惯。我只想允许大写和数字..没有别的
  • “Eh”,或者让我说“Äh”,为什么大写的 A-Umlaut 没有大写字符? +uppercaseLettersCharacterSet 是干什么用的?

标签: ios objective-c uitextview


【解决方案1】:

最简单的解决方案:

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

【讨论】:

    【解决方案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]);
    
    }
    

    【讨论】:

    • 好的,如何将输入的文本与#defined 文本进行比较?
    • 研究使用 NSPredicate。
    • 检查大写 [text isEqualToString:text.uppercaseString]
    • 是的,我同意@sage444。在这种情况下完美的解决方案。
    • 其实这并不能说明@HurkNburkS 只需要字母和数字。
    【解决方案3】:

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

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

    validChars 中的\n 用于返回键(您可能希望也可能不希望允许)。

    按照 cmets 的建议,您可以将小写字母转换为大写字母 自动:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多