<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