注意下文標紅字段:spa
#pragma mark- 輸入改變時ci
- (void) textFieldDidChanged:(UITextField *) TextField{get
//搜索關鍵字一旦改變,將從新搜索,防止下拉的時候數據重複string
_currentPage = 1;it
bool isChinese;//判斷當前輸入法是不是中文io
if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString: @"en-US"]) {select
isChinese = false;搜索
}請求
elsequeue
{
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];
});
}
}