Next we will discuss the content length limit of uitextview under multistage text input (Chinese Input Method)

Source: Internet
Author: User
I have written an article titled how to better limit the input length of a uitextfield. The final conclusion of this article is that it can be directly used.
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;

Listen to and cut out the parts that exceed maxlength.

So when I was dealing with the content length of uitextview, I also directly referred to this method:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];

- (void)textViewDidChange:(NSNotification *)notification{    self.placeholder.hidden = (self.textView.text.length > 0);        if (self.textLengthLimit > 0 && self.textView.text.length > self.textLengthLimit) {        self.textView.text = [self.text substringToIndex:self.textLengthLimit];    }}

After this process, I typed several characters on the keyboard, and no more characters were entered when it reached 200 characters. However, when I copy a lot of Chinese content (more than 200 words) from the web page, paste it into uitextview, and then try to input it, it will go down:
*** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
My solution is:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    if (textView.text.length >= self.textLengthLimit && text.length > range.length) {        return NO;    }        return YES;}
In this way, when the length reaches 200, the input will no longer respond to changes.

However, after the paste is up to 200 characters, you can unbind the text and then use the Chinese Input Method for input. At this time, entering the multistage text input mode (refer to here) will trigger another problem:

*** Terminating app due to uncaught exception ‘NSRangeException‘, reason: ‘NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds‘
Due to the Lenovo and recommendation functions on the keyboard of the Chinese Input Method, the length of the text content may not meet the expectations, resulting in a cross-border. Therefore, you can refer to this answer for further processing:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    if (textView.text.length >= self.textLengthLimit && text.length > range.length) {        return NO;    }        return YES;}- (void)textViewDidChange:(UITextView *)textView{    self.placeholder.hidden = (self.textView.text.length > 0);        if (textView.markedTextRange == nil && self.textLengthLimit > 0 && self.text.length > self.textLengthLimit) {        textView.text = [textView.text substringToIndex:self.textLengthLimit];    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.