- 我的搜索栏上有标签 View ,标题为“12345”
此标签位于 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]];
}
但它不会重新定位我的光标。缺少什么?
最佳答案
在 tableView 的委托(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];
}
关于ios - 在顶部带有标签的 UISearchBar TextField 中重新定位光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25343912/