UITextView中限定字数的中文输入法如何处理?

一、前沿

        项目中有用到TextView,对textView的字数进行限制。那么输入中的联想输入,输入法的连打要怎么监控。比如,连打我我我我我。会被shouldChangeTextInRange给我截取成“wo wo wo”。

二、处理

    1. 当textView.markedTextRange == nil的时候代表没有拼音与输入的字符
    1. 表示textView.markedTextRange != nil的时候代表现在是在进行连续输入。这时候输入的字符无需算进被限制的字符长度之内

/*

  • (void)textViewDidChange:(UITextView *)textView{
    if(textView.markedTextRange == nil){
    NSLog(@"没有预输入的文字--%@", textView.text);
    if(textView.text.length>10){
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入的文字超过10个" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
    textView.text = [textView.text substringToIndex:10];
    }else if([textView.text rangeOfString:@" "].location != NSNotFound){
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请不要输入空格" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
    textView.text = [textView.text substringToIndex:textView.text.length-1];
    }

    }else{
    NSLog(@"有预输入的文字--%@", textView.text);
    }
    }
    */

你可能感兴趣的:(UITextView中限定字数的中文输入法如何处理?)