iOS开发OC -- UITextField、UITextView字数限制

项目中会遇到各种限制字数的需求,简单做个笔记。

//遵循协议
//实时监控
 [_nameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

#pragma mark - TextField Delegate
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    //限制只可以输入数字
    if (textField == _phoneTextField || textField == _registerText) {
        NSCharacterSet * cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
        NSString * filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest) {
            [self showTextView:self.view message:@"请输入数字"];
            return NO;
        }
    }
    return YES;
}

//点击return按键收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}

//简单实现了 点击 某个textField 整体页面上移 保证不会被键盘遮盖
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == _positionTextField ) {
        [UIView animateWithDuration:0.2 animations:^{
            _scrollView.contentOffset = CGPointMake(0, 40);
        }];
    }else if (textField == _telTextField)
    {
        [UIView animateWithDuration:0.2 animations:^{
            _scrollView.contentOffset = CGPointMake(0, 50);
        }];
    }else if (textField == _emailTextField)
    {
        [UIView animateWithDuration:0.2 animations:^{
            _scrollView.contentOffset = CGPointMake(0, 60);
        }];
    }
}

//限制输入字数
- (void)textFieldDidChange:(UITextField *)textField
{
    NSInteger kMaxLength = 10 ;    
    NSString *toBeString = textField.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; //ios7之前使用[UITextInputMode currentInputMode].primaryLanguage
    if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if (!position) {// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
            if (toBeString.length > kMaxLength) {
                textField.text = [toBeString substringToIndex:kMaxLength];
                [self showTextView:self.view message:[NSString stringWithFormat:@"最多可输入%ld位",(long)kMaxLength]];
            }
        }
        else{//有高亮选择的字符串,则暂不对文字进行统计和限制
        }
    }else{//中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
        if (toBeString.length > kMaxLength) {
            textField.text = [toBeString substringToIndex:kMaxLength];
            [self showTextView:self.view message:[NSString stringWithFormat:@"最多可输入%ld位",(long)kMaxLength]];
        }
    }
}


#pragma mark - TextView Delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return
        //在这里做你响应return键的代码
        [self.view endEditing:YES];
        return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
    }
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [UIView animateWithDuration:0.2 animations:^{
        _scrollView.contentOffset = CGPointMake(0, 70);
    }];
    
    //粗略的给textView加上placeholder
    if([textView.text isEqualToString:@"地址"]){
        textView.text=@"";
        textView.textColor = MakeColorRGB(0x7a8fac);
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger kMaxLength = 200 ;
    NSString *toBeString = textView.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; //ios7之前使用[UITextInputMode currentInputMode].primaryLanguage
    if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        if (!position) {// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
            if (toBeString.length > kMaxLength) {
                textView.text = [toBeString substringToIndex:kMaxLength];
            [self showTextView:self.view message:[NSString stringWithFormat:@"最多可输入%ld位",(long)kMaxLength]];
            }
        }
        else{//有高亮选择的字符串,则暂不对文字进行统计和限制
        }
    }else{//中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
        if (toBeString.length > kMaxLength) {
            textView.text = [toBeString substringToIndex:kMaxLength];
            [self showTextView:self.view message:[NSString stringWithFormat:@"最多可输入%ld位",(long)kMaxLength]];
        }
    }
}

你可能感兴趣的:(iOS开发OC -- UITextField、UITextView字数限制)