UITextField limits the number of Chinese characters in the most correct posture, solves the crash caused by the substringToIndex method under iOS7, substringtoindex

Source: Internet
Author: User

UITextField limits the number of Chinese characters in the most correct posture, solves the crash caused by the substringToIndex method under iOS7, substringtoindex

Today, I am writing a requirement to limit the number of UITextField Chinese characters. I thought it was a simple requirement and encountered many problems in actual development.

First, when the Lenovo word is input to TextFiled

-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string;

This leads to invalid code with the length limit.

After searching for information, I learned that I can use registrationUITextFieldTextDidChangeNotificationNotifications to monitor TextField text changes

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)           name:@"UITextFieldTextDidChangeNotification" object:myTextField];

  

In 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 we input Chinese Characters in iOS7, the input will crash immediately once the length reaches the maximum length (in this case, the input state is still pinyin.

* ** Terminating app due to uncaught exception 'nsangeexception', reason: 'nsmutablerlearray replaceObjectsInRange: withObject: length: Out of bounds'

This problem does not occur in systems above iOS 8. It may have been fixed by Apple.

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

This blog solves the problem of garbled characters when UItextFiled intercepts strings (because emoji actually occupies three lengths ).

UITextField * textField = (UITextField *) obj. object; NSString * toBeString = textField. text; // obtain the highlighted UITextRange * selectedRange = [textField markedTextRange]; UITextPosition * position = [textField positionFromPosition: selectedRange. start offset: 0]; // if (! Position) {if (toBeString. length> MAX_STARWORDS_LENGTH) {nsange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex: MAX_STARWORDS_LENGTH]; if (rangeIndex. length = 1) {textField. text = [toBeString substringToIndex: MAX_STARWORDS_LENGTH];} else {nsange rangeRange = [toBeString Duration: NSMakeRange (0, MAX_STARWORDS_LENGTH)]; textField. text = [toBeString substringWithRange: rangeRange] ;}}

After the test, we found that this method works well in systems above the iOS8, but it is completely invalid under iOS7.

The reason is that in iOS7, the position object is always not nil, resulting in code that does not process the length.

After research, we need to add a judgment condition,

Improved code:

UITextField * textField = (UITextField *) obj. object; NSString * toBeString = textField. text; // obtain the highlighted UITextRange * selectedRange = [textField markedTextRange]; UITextPosition * position = [textField positionFromPosition: selectedRange. start offset: 0]; // if (! Position |! SelectedRange) {if (toBeString. length> MAX_STARWORDS_LENGTH) {nsange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex: MAX_STARWORDS_LENGTH]; if (rangeIndex. length = 1) {textField. text = [toBeString substringToIndex: MAX_STARWORDS_LENGTH];} else {nsange rangeRange = [toBeString Duration: NSMakeRange (0, MAX_STARWORDS_LENGTH)]; textField. text = [toBeString substringWithRange: rangeRange] ;}}

No crash problem found after the test again.

 

Related Article

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.