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