UITextView 字数限制,正在输入字符也算进去,区分中英文字符

一、不区分中英文,直接取length

把正在输入的字符也加上。是否超过界限


#pragma mark -键入Done时,插入换行符,然后执行addBookmark
#pragma mark -UITextView delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //判断加上输入的字符,是否超过界限
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > 140)
    {
        if (textAlert == nil) {
            textAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符个数不能大于140" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            textAlert.tag = 19;
            [textAlert show];
        }
        textView.text = [textView.text substringToIndex:140];
        return NO;
    }
    return YES;
}


二、区分中英文,一个中文占3个字节

int len = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
            NSLog(@"==1 text:%@,length:%d",textField.text,len);

一个中文占3个字节,其他是1个


代码可先写在上面这个函数里,但是不好控制,你们试试知道了,我是用另一种方法做的,如下:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextViewTextDidChangeNotification" object:contentTextView];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextViewTextDidChangeNotification" object:contentTextView];
}

-(void)textFiledEditChanged:(NSNotification *)obj{
    UITextView *textField = (UITextView *)obj.object;
    
    
    NSString *alertMessage = [NSString stringWithFormat:@"最大字符数为%d",self.maxWord];
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
        if (!position) {
            int len = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
            NSLog(@"==1 text:%@,length:%d",textField.text,len);
            if (len > self.maxWord) {
                if (!textAlert) {
                    textAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:alertMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [textAlert show];
                }
                else{
                    [textAlert show];
                }
                textField.text = lastContentString;
            }
            else{
                isAboveMaxWord = NO;
                lastContentString = toBeString;
            }
        }
        // 有高亮选择的字符串,则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
    else{
        int len = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"==2 text:%@,length:%d",textField.text,len);
        if (len > self.maxWord) {
            if (!textAlert) {
                textAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:alertMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [textAlert show];
            }
            else{
                [textAlert show];
            }
            textField.text = lastContentString;
        }
        else{
            lastContentString = toBeString;
        }
    }
}

注意:lastContentString  是定义的一个NSString,用来记录最后输入的字符串,因为直接通过substringToIndex:去截取字符串会报错,因为我们输入中文计算长度是转码了的!




你可能感兴趣的:(iOS)