Uitextview the limit of character length when inputting Chinese input method

Source: Internet
Author: User

<1> register at INIT notification: [[Nsnotificationcenter Defaultcenter]addobserver:selfselector: @selector ( Textfilededitchanged:)
name:@ "Uitextfieldtextdidchangenotification"
Object:mytextfield]; <2> implementation of the Monitoring method:-(void) textfilededitchanged: (nsnotification *) obj{
Uitextfield *textfield = (Uitextfield *) Obj.object;

NSString *tobestring = Textfield.text;
NSString *lang = [[Uitextinputmode currentinputmode] primarylanguage]; Keyboard Input Mode
if ([lang isequaltostring:@ "Zh-hans"]) {//Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting
Uitextrange *selectedrange = [TextField markedtextrange];
Get highlighted parts
Uitextposition *position = [TextField positionfromposition:selectedrange.startoffset:0];
Word count and limit the words you have entered without highlighting the selected words
if (!position) {
if (Tobestring.length > Kmaxlength) {
Textfield.text = [tobestring substringtoindex:kmaxlength];
}
}
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 > Kmaxlength) {
Textfield.text = [tobestring substringtoindex:kmaxlength];
}
}
} <3> write off the listening method in Dealloc, remember! -(void) dealloc{
[[Nsnotificationcenter Defaultcenter]removeobserver:self
name:@ "Uitextfieldtextdidchangenotification"
Object:_albumnametextfield];
} Note: Original address http://blog.sina.com.cn/s/blog_60f977e70101g4gj.html

Uitextview the limit of character length when inputting Chinese input method

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.