-(void)textFiledEditChanged{server
//檢測文本改變字符串
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(putInTextFieldEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:self.textField];string
}it
#define kMaxLength 16io
-(void)putInTextFieldEditChanged:(NSNotification *)obj {object
UITextField *textField = (UITextField *)obj.object;select
NSString *toBeString = textField.text;im
// 鍵盤輸入模式統計
NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage;di
if ([lang isEqualToString:@"zh-Hans"]) { // 簡體中文輸入,包括簡體拼音,健體五筆,簡體手寫
UITextRange *selectedRange = [textField markedTextRange];
//獲取高亮部分
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 沒有高亮選擇的字,則對已輸入的文字進行字數統計和限制
if (!position) {
if (toBeString.length > kMaxLength) {
textField.text = [toBeString substringToIndex:kMaxLength];
}
}
// 有高亮選擇的字符串,則暫不對文字進行統計和限制
else{
}
}
// 中文輸入法之外的直接對其統計限制便可,不考慮其餘語種狀況
else{
if (toBeString.length > kMaxLength) {
textField.text = [toBeString substringToIndex:kMaxLength];
}