static const NSInteger kMaxLength = 15;
[self.NickName addTarget:self action:@selector(textFieldChange) forControlEvents:UIControlEventEditingChanged];
//限定主题字数 15个
- (void)textFieldChange{
NSString *toBeString = self.NickName.text;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage];
if([lang isEqualToString:@"zh-Hans"]){ //简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [self.NickName markedTextRange];
UITextPosition *position = [self.NickName positionFromPosition:selectedRange.start offset:0];
if (!position){//非高亮
if (toBeString.length > kMaxLength) {
[SVProgressHUD showImage:nil status:@"您最多可以输入15个字符"];
self.NickName.text = [toBeString substringToIndex:kMaxLength];
}
}
}else{//中文输入法以外
if (toBeString.length > kMaxLength) {
[SVProgressHUD showImage:nil status:@"您最多可以输入15个字符"];
self.NickName.text = [toBeString substringToIndex:kMaxLength];
}
}
}