IOS "TextView or TextField text input word limit"

Source: Internet
Author: User

"TextView text input word limit"

Listen for text change trigger time (done by sending notification)

-(void) textvieweditchanged: (nsnotification *) obj{

Uitextview *textview = (Uitextview *) Obj.object;

nsattributedstring *tobestring = Textview.attributedtext;

NSLog (@ "--%@", [_contentview.textinputmode primarylanguage]);

Method has not been recommended to use, directly get textinputmode when the expression keyboard does not respond

NSString *lang = [[Uitextinputmode currentinputmode] primarylanguage]; Keyboard Input Mode

if (Tobestring.length > Maxlenth)//maxlenth defined macros

{

[Self showhudwithtext:@ "input word limit 300"];

}

if ([lang isequaltostring:@ "Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting

Uitextrange *selectedrange = [TextView markedtextrange];

Get highlighted parts

Uitextposition *position = [TextView positionFromPosition:selectedRange.start offset:0];

Word count and limit the words you have entered without highlighting the selected words

if (!position) {

if (tobestring.length >= maxlenth) {

Textview.attributedtext = [Tobestring attributedsubstringfromrange:nsmakerange (0, Maxlenth)];

Textview.font = [Uifont systemfontofsize:14.f];

}

if (TextView.attributedText.length > 300)

{

Self.lenthLabel.text = @ "Number of words entered: 300";

}

else Self.lenthLabel.text = [NSString stringwithformat:@ "input words:%@", @ (textView.attributedText.length)];

}

There is a highlighted string, the text is temporarily not counted and limited

else{

}

}

Direct statistical restrictions other than Chinese input method, regardless of other languages

else{

if (Tobestring.length > Maxlenth) {

Textview.attributedtext = [Tobestring attributedsubstringfromrange:nsmakerange (0, Maxlenth)];

Textview.font = [Uifont systemfontofsize:14.f];

}

if (TextView.attributedText.length > 300)

{

Self.lenthLabel.text = @ "Number of words entered: 300";

}

else Self.lenthLabel.text = [NSString stringwithformat:@ "input words:%@", @ (textView.attributedText.length)];

}

}

IOS "TextView or TextField text input word limit"

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.