-(Nsuinteger) Lettercountwithlimits: (Nsinteger) limits {NSString*tobestring =Self.text; Nsuinteger Txtcount=tobestring.length; Uitextrange*selectedrange =[self markedtextrange]; //get highlighted partsUitextposition *position = [Self positionFromPosition:selectedRange.start offset:0]; //Word Count and limit the words you have entered without highlighting the selected words if(!position) { if(Tobestring.length >limits) {Self.text=[tobestring substringtoindex:limits]; } } //a highlighted string to remove the highlighted number of words Else{Nsinteger Startoffset=[self offsetFromPosition:self.beginningOfDocument toPosition:selectedRange.start]; Nsinteger Endoffset=[self offsetFromPosition:self.beginningOfDocument toPosition:selectedRange.end]; Nsrange Offsetrange= Nsmakerange (Startoffset, Endoffset-startoffset); //Remove the highlighted word countTxtcount-=offsetrange.length; } //partial warnings and restrictions exceeded if(Txtcount >limits) {Self.text=[tobestring substringtoindex:limits]; returnlimits; } returnTxtcount;}
In
-(void) Textviewdidchange: (Uitextview *) The method is called in TextView, and the return value is the current word count.
Markedtextrange, remove the highlighted word count, for the Chinese need to do such a deal.
The disadvantage is that it is only a word, and many emoji will think of it as two words.
Share a Uitextview classification method for word limit and statistics