How iOS can limit character length to TextField

Source: Internet
Author: User

During the development of the project, the maximum length of the input must be limited to 24 bytes when editing the label when you encounter such a requirement.

Review some of the materials and refer to the relevant methods of previous projects to summarize the solution here today.

1. Add a listener for the edit Change event for Uitextfield:

[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, kill the last word, and then determine if the limit length is exceeded, so that it loops until the length limit is met. Note here that directly using the length method, the number of characters returned, rather than the number of bytes, lengthofbytesusingencoding can return the number of bytes encoded by the specified character.

In addition, Chinese input, and usually English input will not be the same, Chinese input will appear in Pinyin, have not selected Chinese characters, Pinyin has gone up:



At this point, the pinyin itself may account for more bytes than the characters after the word is selected, such as input Zhong, which accounts for 5 bytes, and if the user chooses "medium", only three bytes. If you do not specifically consider this situation, it will appear clearly can also lose a Chinese character, but the input pinyin is not able to complete the Zhong Pinyin input. So we need to determine if TextField has the selected content at that time.

3. For the diagram in step 2, if you save directly in the state of the diagram, textfiled will also save pinyin that is not converted to Chinese characters. So you also need to do a bit of processing before using the content of TextField, and remove the excess parts:

-(void) repairtextfield{    * temp = self.textField.text;      while (1)    {        if ([temp lengthofbytesusingencoding:nsutf8stringencoding] <= kmaxbyteofalarmname) {              Break ;        }         Else         {            = [temp substringtoindex:temp.length-1];        }    }    Self.textField.text=temp;}

Done. If you have any questions, please note that the message is discussed together.

Add something:

Why not use the Uitextfield proxy method Shouldchangecharactersinrange?

At present, this method can not capture paste, pinyin to Chinese characters and Lenovo (such as the use of pinyin to "China", the candidate directly in the keyboard may choose "Viva") input caused by the change.

How iOS can limit character length to TextField

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.