#pragma mark-uitextview delegate-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (Nsrange) Range replacementstring: (NSString *) string{uitextrange *selectedrange = [TextField markedtextrange]; Get the highlighted part uitextposition *pos = [TextField positionFromPosition:selectedRange.start offset:0]; Get highlighted content//nsstring * SelectedText = [TextView textinrange:selectedrange]; If there is a highlight and the current word start position is less than the maximum limit, allow input if (Selectedrange && pos) {Nsinteger startoffset = [TextField offsetfrompos Ition:textField.beginningOfDocument ToPosition:selectedRange.start]; Nsinteger Endoffset = [TextField offsetFromPosition:textField.beginningOfDocument toPosition:selectedRange.end]; Nsrange Offsetrange = Nsmakerange (Startoffset, Endoffset-startoffset); if (Offsetrange.location < max_starwords_length) {return YES; } else {return NO; }} nsstring *comcatstr =[Textfield.text Stringbyreplacingcharactersinrange:range withstring:string]; Nsinteger Caninputlen = max_starwords_length-comcatstr.length; if (Caninputlen >= 0) {return YES; } else {Nsinteger len = string.length + Caninputlen; Prevents when Text.length + Caninputlen < 0 o'clock, making rg.length an illegal maximum positive number error Nsrange RG = {0,max (len,0)}; if (Rg.length > 0) {nsstring *s = @ ""; Determines whether only ordinary characters or ASC codes (for Chinese and Emoticons return NO) BOOL ASC = [string canbeconvertedtoencoding:nsasciistringencoding]; if (ASC) {s = [string substringwithrange:rg];//because the ASCII code is directly taken it can be no wrong} else {__block Nsinteger idx = 0; __block nsstring *trimstring = @ "";//intercept the string//use the strings traversal, this method can know exactly whether each emoji is a Unicode or two [stri Ng Enumeratesubstringsinrange:nsmakerange (0, [string length]) optionS:nsstringenumerationbycomposedcharactersequences Usingblock: ^ (nsstring* substring, Nsrange Substringrange, Nsrange Enclosingrange, bool* stop) { if (idx >= rg.length) {*stop = YES;//Remove the required break, Improve efficiency return; } trimstring = [trimstring Stringbyappe Ndingstring:substring]; idx++; }]; s = trimstring; //rang refers to the substitution processing from the current cursor (note that if yes is returned after this sentence is executed, the Didchange event is triggered) [TextField settext:[textfield.text Stringbyr Eplacingcharactersinrange:range Withstring:s]]; Since it is beyond the partial interception, which must be the mostThe big limit. Self.lbNums.text = [NSString stringwithformat:@ "%d/%ld", 0, (long) max_starwords_length]; } return NO; }}//calculates the number of converted characters-(Nsuinteger) lenghtwithstring: (NSString *) string{Nsuinteger len = string.length; Chinese Character Set NSString * pattern = @ "[\u4e00-\u9fa5]"; Nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:pattern options: Nsregularexpressioncaseinsensitive Error:nil]; Number of characters in the calculation nsinteger nummatch = [Regex numberofmatchesinstring:string options:nsmatchingreportprogress Range:NSMakeR Ange (0, Len)]; return len + Nummatch;} #pragma mark-notification method-(void) Textfielddidchange: (Uitextfield *) textfield{uitextrange *selectedrange = [Tex Tfield Markedtextrange]; Get the highlighted part uitextposition *pos = [TextField positionFromPosition:selectedRange.start offset:0]; If the highlighted part is changing in the change, do not calculate the character if (Selectedrange && pos) {return; } nsstring *nstextcontent = Textfield.text;Nsinteger existtextnum = nstextcontent.length; if (Existtextnum > Max_starwords_length) {//intercept to the maximum position of the character (because the outside section is processed at should, this is where the efficiency is no longer judged) NSString *s = [Nstextcontent substringtoindex:max_starwords_length]; [TextField settext:s]; }//Do not let display negative port day Self.limitLabel.text = [NSString stringwithformat:@ "%lu", MAX (0,max_starwords_length-textfield.t Ext.length)];}
Keyboard Word limit