Uitextfield limit the number of Chinese characters the most correct posture, solve iOS7 under the Substringtoindex method caused by the crash

Source: Internet
Author: User

Today is writing a requirement to limit the number of Uitextfield kanji, a requirement that was thought to be very simple, and encountered a lot of problems in actual development.

First of all, the input of Chinese characters when the associative words in the input to textfiled, and will not go

-(BOOL) TextField: (uitextfield *) TextField shouldchangecharactersinrange: (nsrange) range Replacementstring: (nsstring *) string;

This causes the code that is here to be the length limit to fail. Blocked.

after finding the information, we know that we can use the registration UITextFieldTextDidChangeNotification notifications to monitor changes in TextField text

[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (textfilededitchanged:)           name:@ " Uitextfieldtextdidchangenotification "Object:mytextfield";

  

In the textfilededitchanged: method

-(void) textdidchanged: (nsnotification *) notification{        Nsinteger maxLength = 6;    Uitextfield *textfield = (Uitextfield *) Notification.object;    if (TextField.text.length > MaxLength)    {        Textfield.text = [Textfield.text substringtoindex:maxlength];}    }

This method does not seem to be a problem, however, when the measured input characters in iOS7, once the length reaches the maximum length, and then input (the input State or pinyin) will immediately crash.

Terminating app due to uncaught exception ' nsrangeexception ', Reason: ' Nsmutablerlearray replaceobjectsinrange: Withobject:length:: Out of Bounds '

This problem does not occur in systems above iOS8, and it may be that Apple fixed the bug.

Find the information again and find this blog http://www.jianshu.com/p/2d1c06f2dfa4

This blog solves the problem of uitextfiled when intercepting a string, if the interception of emoji is garbled (because emoji actually takes up 3 lengths).

Uitextfield *textfield = (Uitextfield *) obj.Object; NSString*tobestring =Textfield.text; //get highlighted partsUitextrange *selectedrange =[TextField Markedtextrange]; Uitextposition*position = [TextField positionFromPosition:selectedRange.start offset:0]; //Word Count and limit the words you have entered without highlighting the selected words    if(!position) {        if(Tobestring.length >max_starwords_length) {Nsrange Rangeindex=[tobestring rangeofcomposedcharactersequenceatindex:max_starwords_length]; if(Rangeindex.length = =1) {Textfield.text=[tobestring substringtoindex:max_starwords_length]; }            Else{nsrange Rangerange= [Tobestring Rangeofcomposedcharactersequencesforrange:nsmakerange (0, Max_starwords_length)]; Textfield.text=[tobestring Substringwithrange:rangerange]; }        }    }

After the test, it was found that the system works well in iOS8 and above, but it completely failed under iOS7.

The reason is that under IOS7, the position object is always not nil, resulting in code that does not go into the processing length.

It is now necessary to add a condition of judgment

The improved Code:

Uitextfield *textfield = (Uitextfield *) Obj.object;    NSString *tobestring = Textfield.text;    Get the highlighted part    uitextrange *selectedrange = [TextField markedtextrange];    Uitextposition *position = [TextField positionFromPosition:selectedRange.start offset:0];    No highlighted word, word count and limit    if (!position | |!selectedrange)    {        if (Tobestring.length > Max_starwords _length)        {            Nsrange rangeindex = [tobestring rangeofcomposedcharactersequenceatindex:max_starwords_length];            if (rangeindex.length = = 1)            {                Textfield.text = [tobestring substringtoindex:max_starwords_length];            }            else            {                Nsrange rangerange = [tobestring rangeofcomposedcharactersequencesforrange:nsmakerange (0, MAX_ Starwords_length)];                Textfield.text = [tobestring substringwithrange:rangerange];}}    

No crashes were found after testing again.

Uitextfield limit the number of Chinese characters the most correct posture, solve iOS7 under the Substringtoindex method caused by the crash

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.