一、只能输入纯数字
在这里以UITextField为例:其实现代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { return [self validateNumber:string]; } - (BOOL)validateNumber:(NSString*)number { BOOL res = YES; NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@ "0123456789" ]; int i = 0 ; while (i < number.length) { NSString * string = [number substringWithRange:NSMakeRange(i, 1 )]; NSRange range = [string rangeOfCharacterFromSet:tmpSet]; if (range.length == 0 ) { res = NO; break ; } i++; } return res; } |
另外我们还有一种更加简便的方法来实现这一目的:
首先宏定义
1 | #define NUMBER @ "0123456789" |
接着
1 2 3 4 5 6 | - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBER] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@ "" ]; return [string isEqualToString:filtered]; } |
二、只能输入纯大小写字母
和以上只能输入纯数字类似,实现起来简单,只需要宏定义
1 | #define LETTER @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
然后实现
1 2 3 4 5 6 | - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LETTER] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@ "" ]; return [string isEqualToString:filtered]; } |
三、大小写字母和数字结合输入
对照以上
1 | #define LETTER_NUMBER @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |
同样道理具体能够输入那些内容如果输入内容能够一一列举的话我们就可以通过define来设置了,实现起来超简单。
限制只能输入中文
在这里用到了观察者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | - ( void )viewDidLoad { [ super viewDidLoad]; _myTextField.delegate = self; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:UITextFieldTextDidChangeNotification object:_myTextField]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; //过滤非汉字字符 textField.text = [self filterCharactor:textField.text withRegex:@ "[^\u4e00-\u9fa5]" ]; if (textField.text.length >= 4 ) { textField.text = [textField.text substringToIndex: 4 ]; } return NO; } - ( void )textFiledEditChanged:(id)notification{ UITextRange *selectedRange = _myTextField.markedTextRange; UITextPosition *position = [_myTextField positionFromPosition:selectedRange.start offset: 0 ]; if (!position) { 没有高亮选择的字 //过滤非汉字字符 _myTextField.text = [self filterCharactor:_myTextField.text withRegex:@ "[^\u4e00-\u9fa5]" ]; if (_myTextField.text.length >= 4 ) { _myTextField.text = [_myTextField.text substringToIndex: 4 ]; } } else { //有高亮文字 //do nothing } } - (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{ NSString *searchText = string; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error]; NSString *result = [regex stringByReplacingMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange( 0 , searchText.length) withTemplate:@ "" ]; return result; } - ( void )dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } |
如果要限制输入字符位数的话可以直接设置,这个实现上有很多种,最简单的就是
1 2 3 4 5 6 | - ( void )textViewDidChange:(UITextView *)textView{ NSInteger number = [textView.text length]; if (number > 300 ) { textView.text = [textView.text substringToIndex: 300 ]; } } |