ios - 在顶部带有标签的 UISearchBar TextField 中重新定位光标

标签 ios objective-c

  • 我的搜索栏上有标签 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/

相关文章:

ios - 在 CATextLayer 中显示属性字符串

ios - os_log timeval格式的Clang错误

objective-c - 是否应该尽可能避免反转 NSMutableArray?

ios - 为什么我用于 UIBarButtonItem 的 UIButton 的高度没有变化?

ios - sleep NSThread 与。 GCD 调度后

ios - 当检测到的图像不再被捕获时停止播放视频

objective-c - iOS 6 旋转问题 - 呈现模态视图 Controller 没有旋转

ios - Apple - 如果密码更改,我们如何在 iOS 应用程序中使用 Touch ID

ios - iPhone - 我们能否判断通知样式是否设置为 'Banner' 还是 'Alerts'

ios - MoPub 原生广告的 UITableViewCell 问题