IOS restricts the length of textField characters. iostextfield

Source: Internet
Author: User

IOS restricts the length of textField characters. iostextfield

In the project development process, when editing tags, the maximum length of input must be limited to 24 bytes.

I have consulted some materials and referred to the solutions of previous projects. I will summarize the solutions here today.

1. Add a listener for UITextField editing change events:

 

[self.textField addTarget:self action:@selector(textLengthChange:) forControlEvents:UIControlEventEditingChanged];

 

2. Implement the corresponding action:

 

-(void)textLengthChange:(id)sender{    UITextField * textField=(UITextField*)sender;    NSString * temp = textField.text;        if (textField.markedTextRange ==nil)    {        while(1)        {            if ([temp lengthOfBytesUsingEncoding:NSUTF8StringEncoding] <= kMaxByteOfAlarmName) {                break;            }            else            {                temp = [temp substringToIndex:temp.length-1];            }        }        textField.text=temp;    }}

 

Note: If the content is too long, the last word is killed and the limit length is exceeded until the limit length is met. Note that the length method is used directly to return the number of characters instead of the number of bytes. lengthOfBytesUsingEncoding can return the number of bytes encoded by the specified character.

In addition, Chinese characters may be different in Chinese and Chinese characters. Chinese characters may be input in pinyin, but Chinese characters are not selected yet. Pinyin has already been entered:

At this time, the number of bytes occupied by pinyin itself may be greater than the number of bytes occupied by the selected Chinese characters. For example, the number of characters entered by zhong occupies 5 bytes. If you select medium, only three bytes are occupied. If you do not specifically consider this situation, you may still be able to enter another Chinese character, but you cannot complete the Pinyin input of zhong. Therefore, you need to determine whether textField has the selected content at that time.

3. For the graph in step 2, if it is saved directly when the graph appears, textfiled will save the pinyin that is not converted to Chinese characters. Therefore, you need to process the content of textfield and delete the excess parts:

 

-(void)repairTextField{    NSString * temp = self.textField.text;    while(1)    {        if ([temp lengthOfBytesUsingEncoding:NSUTF8StringEncoding] <= kMaxByteOfAlarmName) {            break;        }        else        {            temp = [temp substringToIndex:temp.length-1];        }    }    self.textField.text=temp;}

 

Done. If you have any questions, leave a message and discuss them together.

 

Something to add:

Why doesn't I need the UITextField proxy method shouldChangeCharactersInRange?

Currently, this method cannot capture and paste, convert Chinese characters into Chinese characters, and associate them with Chinese characters (for example, if you use Pinyin to input "China", you can choose "Long live" from the candidate words on the keyboard ") input.

 

 

 

 

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.