IOS limit Uitextfield Input characters

Source: Internet
Author: User

begins

Before doing an instant communication project, it is necessary to limit the number of characters in the text of the input box, then search for several methods from the network, solve the demand, but the solution on the network is not very comprehensive; and today we have a problem restricting the search box uisearchbar the number of input characters, So just want to put the character limit this problem to do a complete and thorough point, below I put forward a few questions, and then one after another to do the answer, to see if you do the character restrictions when you encounter the same problem. Before I ask these questions, let me introduce a few methods:

-(BOOL) Searchbar: (Uisearchbar *) Searchbar Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text

This method is the proxy method of the Uisearchbar, which is triggered before the text substitution, the return value of the bool type, if the return is yes, the substitution, or vice versa, is not replaced.

-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText

This method is the proxy method for Uisearchbar, which is triggered after the text input is complete, and the return value is null (no return value).

We're going to do the character-limiting work in these two callback methods; Let's call the first method method one, the second method is called method two, and the method one and method two mentioned below represent both methods.

PS: Explain why I use Uisearchbar proxy method, rather than Uitextfield proxy method, this is because I used to study the Uisearchbar, therefore, in writing this blog when the use of Uisearchbar, Instead of Uitextfield, but the input box in the Uiseachbar is actually Uitextfield, so it's the same with Uisearchbar for the input character limits, and finally I'll explain the difference with Uitextfield. OK, now let's get to the point.

problem

1. Only the character limit in method one, what happens when I do Lenovo input in Chinese input method

2. Do character restrictions only in method one, what happens when pasting characters into a text box

3. Only in method two to do the character limit, in the Chinese input method, highlight the input of what will be the problem

Now that I have put the problem forward, we will solve these problems.

The simplest way to restrict input characters is to not distinguish between English and Chinese characters when counting the characters entered, as in the following example:

-(BOOL) Searchbar: (Uisearchbar *) Searchbar Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text {

uitextfield* TextField = [_searchbar valueforkey:@ "_searchfield"];

if (TextField.text.length > Kmaxlength) {
Textfield.text = [Textfield.text substringtoindex:kmaxlength];
}

return YES;

}

This will appear in my question 1 and question 2, when entered in the Chinese input method, the selected characters are not called this method, so the task of counting the number of characters has failed, the same way, paste the text will not call this method. So how do you solve these two problems? is to find a way to still invoke the method in both cases; Obviously, the two methods I mentioned have worked, and now we have changed as follows:

-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText {

uitextfield* TextField = [_searchbar valueforkey:@ "_searchfield"];

if (TextField.text.length > Kmaxlength) {
Textfield.text = [Textfield.text substringtoindex:kmaxlength];
}

}

However, this will be the problem I mentioned above 3, in the Chinese input method input characters, the highlighted character input will not call this method, so the statistical character input is a little bit of a problem. How do we deal with it to achieve our goal? Now I'm going to make the following changes to the code:

-(BOOL) Searchbar: (Uisearchbar *) Searchbar Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text {

if (Text.length = = 0) {
return YES;
}

NSString *lang = [[Uitextinputmode currentinputmode] primarylanguage]; Keyboard Input Mode
if ([lang isequaltostring:@ "Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting

uitextfield* TextField = [_searchbar valueforkey:@ "_searchfield"];

if (TextField.text.length > Kmaxlength) {
return NO;
}

}

return YES;

}

-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText {

uitextfield* TextField = [_searchbar valueforkey:@ "_searchfield"];

if (TextField.text.length > Kmaxlength) {
Textfield.text = [Textfield.text substringtoindex:kmaxlength];
}

}

Okay, the three questions that are now being addressed are resolved.

Expand

If the statistical characters are to be distinguished between English and Chinese, then this can be done:

-(BOOL) Searchbar: (Uisearchbar *) Searchbar Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text {

if (Text.length = = 0) {
return YES;
}

NSString *lang = [[Uitextinputmode currentinputmode] primarylanguage]; Keyboard Input Mode
if ([lang isequaltostring:@ "Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting

uitextfield* TextField = [_searchbar valueforkey:@ "_searchfield"];

if (TextField.text.length > Kmaxlength) {
return NO;
}

}

return YES;

}

-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText {

nsstring* Text = [self textlength:searchtext];
Searchbar.text = text;

}

-(nsstring*) TextLength: (NSString *) text//Calculate string length
{
int number = 0.0;
for (int index = 0; index < [text length]; index++)
{
NSString *character = [text Substringwithrange:nsmakerange (index, 1)];

if ([character lengthofbytesusingencoding:nsutf8stringencoding] = = 3) {
number+=2;
} else {
number++;
}
if (kmaxlength = = number) {
return [text substringwithrange:nsmakerange (0, Index + 1)];
}
}
return text;
}

Such a Chinese will represent two English, this can be changed according to the needs of their own.

Finally say Uitextfield and uisearchbar different places, in fact, is the proxy method is not the same, Method-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (NSString *) The function of string is the same as method one While Uitextfield does not have the same method as method two, but it is by a notification uitextfieldtextdidchangenotification, the function of this notification is the same as method two, so as long as the registration of this notification, the implementation of the code in the second can be. One more thing you need to do is to remember to remove the notification after you've finished using the notification. (ps:lengthofbytesusingencoding: Want to learn some coding knowledge can see my other article http://www.cnblogs.com/wfwenchao/p/4795268.html)

All the work to limit the input characters to this point is complete.

IOS limit Uitextfield Input characters

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.