在UITextView中防止小写
我试图阻止用户input任何小写字符到UITextView中。
我已经创build了可接受字符的#define字符集
#define VALID_CHARACTERS @"ABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890"
现在我想知道如何使用这个在UITextView委托中返回no时,当用户试图input一个字符,不符合我的VALID_CHARACTERS
任何帮助,将不胜感激。
要限制文本视图的可能input字符,请像这样实现文本视图委托:
- (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
是用于返回键(你可能不希望允许)。
正如评论中所build议的那样,您可以自动将小写字母转换为大写字母:
- (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
的定义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; }
我build议使用textView:shouldChangeTextInRange:replacementText:
并且如果replace文本包含无效字符,则返回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, ie 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 (ie no invalid characters // have been removed), return yes, if not, return no. return ([filteredString isEqualToString:string]); }
最简单的解决scheme
使用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; }