这篇文章给大家分享的是有关iOS中输入框如何设置指定字符输入功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
一、只能输入纯数字
在这里以UITextField为例:其实现代码如下:
- (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; }
另外我们还有一种更加简便的方法来实现这一目的:
首先宏定义
#define NUMBER @"0123456789"
接着
- (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]; }
二、只能输入纯大小写字母
和以上只能输入纯数字类似,实现起来简单,只需要宏定义
#define LETTER @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
然后实现
- (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]; }
三、大小写字母和数字结合输入
对照以上
#define LETTER_NUMBER @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
同样道理具体能够输入那些内容如果输入内容能够一一列举的话我们就可以通过define来设置了,实现起来超简单。
限制只能输入中文
在这里用到了观察者(更多观察者模式的介绍参考这里:https://www.jb51.net/article/76122.htm)
- (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]; }
如果要限制输入字符位数的话可以直接设置,这个实现上有很多种,最简单的就是
- (void)textViewDidChange:(UITextView *)textView{ NSInteger number = [textView.text length]; if (number > 300) { textView.text = [textView.text substringToIndex:300]; } }
感谢各位的阅读!关于“iOS中输入框如何设置指定字符输入功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。