首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UISearchBar TextField中重新定位光标,顶部有标签

在UISearchBar TextField中重新定位光标,顶部有标签
EN

Stack Overflow用户
提问于 2014-08-16 20:55:56
回答 1查看 226关注 0票数 0
  • 我的searchBar上有标签视图,标题为"12345“
  • 此标签位于textField部件UISearchBar的顶部。
  • 现在,我试图将光标定位在标签"12345“之后。

我做了这个:

代码语言:javascript
复制
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

代码语言:javascript
复制
+ (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]];
}

但它不会重新定位我的光标。少了什么?

EN

回答 1

Stack Overflow用户

发布于 2014-08-16 22:50:29

在tableView`s的委托方法中创建标签,将标签添加到UISearch栏中。

代码语言:javascript
复制
- (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];


}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25343912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档