注意下文标红字段:
#pragma mark- 输入改变时
- (void) textFieldDidChanged:(UITextField *) TextField{
//搜索关键字一旦改变,将重新搜索,防止下拉的时候数据重复
_currentPage = 1;
bool isChinese;//判断当前输入法是否是中文
if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString: @"en-US"]) {
isChinese = false;
}
else
{
isChinese = true;
}
//去掉搜索中所有空格
NSString *seachtext = [[_seachtextField text] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@",seachtext);
_seachtextField.text=seachtext;
if (isChinese) { //中文输入法下
UITextRange *selectedRange = [TextField markedTextRange];
//获取高亮部分
UITextPosition *position = [TextField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行请求
if (!position) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (_seachtextField.text.length>50) {
[ApplicationDelegate showMsg:@"输入搜索地址过长!" inView:ApplicationDelegate.window];
_seachtextField.text=[_seachtextField.text substringWithRange:NSMakeRange(0, 49)];
return ;
}
DLog(@"输入的英文转化为汉字的状态");
// [self headerRereshing];
});
}else
{
DLog(@"复制粘贴很多文字,继续输入会导致崩溃:联想输入时输入的英文还没有转化为汉字的状态会导致崩溃");
//对超出限制长度情况进行判断,并直接退出
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (_seachtextField.text.length>50) {
_seachtextField.text=[_seachtextField.text substringWithRange:NSMakeRange(0, 49)];
return ;
}
return;
});
}
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (_seachtextField.text.length>50) {
[ApplicationDelegate showMsg:@"输入搜索地址过长!" inView:ApplicationDelegate.window];
_seachtextField.text=[_seachtextField.text substringWithRange:NSMakeRange(0, 49)];
return ;
}
DLog(@"输入英文的状态");
// [self headerRereshing];
});
}
}