美文网首页代码片段iOS相关键盘
iOS 设置输入框的指定字符输入

iOS 设置输入框的指定字符输入

作者: 见哥哥长高了 | 来源:发表于2018-01-09 14:01 被阅读100次

    很多情况下,一下输入框需要按照要求进行输入,输入内容由开发人员来指定。例如:密码输入框只能输入纯数字或者是拼音与数字结合的文本等,那么我们在开发的时候就需要做一些输入文本的限时。

    一、只能输入纯数字

    在这里以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来设置了,实现起来超简单。

    限制只能输入中文

    在这里用到了观察者

    - (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 设置输入框的指定字符输入

        本文链接:https://www.haomeiwen.com/subject/gjiwnxtx.html