此标签位于UISearchBar的textField部分之上
现在我正在尝试将光标定位在标签“12345”
我这样做了:
UITextField *searchBarTextField = [self.searchField valueForKey: @"_searchField"];
UITextRange *selRange = searchBarTextField.selectedTextRange;
UITextPosition *selStartPos = selRange.start;
NSInteger idx = [searchBarTextField offsetFromPosition:searchBarTextField.beginningOfDocument toPosition:selStartPos];
idx = idx + 150;
[Util selectTextForInput:searchBarTextField atRange:NSMakeRange(idx, 50)];
Util.m
+ (void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
UITextPosition *start = [input positionFromPosition:[input beginningOfDocument]
offset:range.location];
UITextPosition *end = [input positionFromPosition:start
offset:range.length];
[input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
}
但它没有重新定位我的光标。缺少什么?
答案 0 :(得分:1)
在tableView`s delegate中创建标签方法将标签添加到UISearch栏。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//get the content of selected cell
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
// now you can get the any content of the selected cell , for example
//Create your label
UILabel *label = [UILabel alloc] init];
label.text = selectedCell.label.text;
/...customize your label as you wish .../
//lastly add the label to the UISearch
[yourSearchBar addSubView: label];
}