IOS TextField (textview) word count

Source: Internet
Author: User

TextField sometimes need to set the word limit, if it is only English or symbols, you can directly in the following proxy methods to determine the number of words

-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *)string{    return  YES;}

However, if it is Chinese and the Lenovo is turned on, the words that may be selected (highlighted) will also be added to the word count, resulting in the last few word input failures
Workaround add an event to TextField to listen for text changes in real time

[TextField addtarget:self Action: @selector (Textfielddidchange:) forcontrolevents:uicontroleventeditingchanged];
(void) Textfielddidchange: (uitextfield*) textfield{NSString*lang = TextField.textInputMode.primaryLanguage;//Keyboard Input Mode    if([lang isequaltostring:@"Zh-hans"]) {//Chineseif(TextField = =_nicktextfield) {Uitextrange*selectedrange =[TextField Markedtextrange]; if(!selectedrange) {//No highlighting                if(textfield.text.length>8) {Textfield.text= [Textfield.text Substringtoindex:8]; [Progresshud showwithstatus:@"nickname up to 8 bits"]; }            }        }    }Else{//non-Chineseif(textfield.text.length>8&&textfield = =_nicktextfield) {Textfield.text= [Textfield.text Substringtoindex:8]; [Progresshud showwithstatus:@"nickname up to 8 bits"]; }    }    if(textfield==_nicktextfield) {NSLog (@"_nicktextfield:%@", _nicktextfield.text); }Else if(textfield==_passwordtextfield) {NSLog (@"_passwordtextfield:%@", _passwordtextfield.text); }Else{            }}

Compared to TextField, TextView is more advanced, he directly provides a text change to complete the proxy method

-(void) Textviewdidchange: (Uitextview *) textview{if(textView.text.length) {_placelabel.hidden=YES; }Else{_placelabel.hidden=NO; } nsstring*lang = TextView.textInputMode.primaryLanguage;//Keyboard Input Mode    StaticNsinteger length =0; if([lang isequaltostring:@"Zh-hans"]) {Uitextrange*selectedrange =[TextView Markedtextrange]; if(!selectedrange) {//There is no highlightLength =textView.text.length; }Else{                    }    }Else{length=textView.text.length; }}

IOS TextField (textview) word count

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.