Limit TextField length
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(textFiledEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:self.textField];
#define KMAXLENGTH-(void) Textfilededitchanged: (nsnotification*) obj{[ SelfSetneedsstatusbarappearanceupdate]; Uitextfield *textfield = (Uitextfield *) obj. Object;NSString*tobestring = TextField. Text;NSString*lang = [[[Uitextinputmode Activeinputmodes] firstobject]primarylanguage];//Keyboard input mode if([Lang isequaltostring:@"Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwritingUitextrange *selectedrange = [TextField markedtextrange];//Get highlighted partsUitextposition *position = [TextField positionfromposition:selectedrange. StartOffset0];//No highlighted words, word count and limit for typed text if(!position) {if(tobestring. Length> kmaxlength) {TextField. Text= [Tobestring substringtoindex:kmaxlength]; } }//With a highlighted string, the text is temporarily not counted and restricted Else{ } }//In addition to the Chinese input method directly to its statistical restrictions, regardless of other languages Else{if(tobestring. Length> kmaxlength) {TextField. Text= [Tobestring substringtoindex:kmaxlength]; } }}
IOS uitextfield-Continuous Update