NSString *lang = [self.inputTextField.textInputMode primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"])
{ // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [self.inputTextField markedTextRange]; //获取高亮部分
//获取已经确认的文字的长度
NSInteger confirmedlength = [self.inputTextField offsetFromPosition:self.inputTextField.beginningOfDocument
toPosition:selectedRange.start];
if ((nil == selectedRange) || selectedRange.isEmpty)
{
confirmedlength = self.inputTextField.text.length;
}
//截断超过长度的部分
BOOL bCuttedOff = NO;
//处理一个字符长度超过多个的情况
if (confirmedlength >= 15)
{
NSRange rangeRange = [self.inputTextField.text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 15)];
if (self.inputTextField.text.length > rangeRange.length)
{
bCuttedOff = YES;
self.inputTextField.text = [self.inputTextField.text substringSafeWithRange:rangeRange];
}
}
if (YES == bCuttedOff)
{
//太长了
}
return;
}
else
{
BOOL bCuttedOff = NO;
if (self.inputTextField.text.length > 15)
{
//处理一个字符长度超过多个的情况
NSRange rangeRange = [self.inputTextField.text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 15)];
if (self.inputTextField.text.length > rangeRange.length)
{
bCuttedOff = YES;
self.inputTextField.text = [self.inputTextField.text substringSafeWithRange:rangeRange];
}
}
if (YES == bCuttedOff)
{
//太长了
}
return ;
}