Share a Uitextview classification method for word limit and statistics

Source: Internet
Author: User

-(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

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.