通过编程设置时不显示UITextView selectedRange [英] UITextView selectedRange not displaying when set programmatically

查看:188
本文介绍了通过编程设置时不显示UITextView selectedRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,以编程方式设置UITextView的selectedRange 会选择来选择文本,但是 不会 会在视觉上指示它已被选中.这听起来像OS 3.0/3.1中的错误吗?更重要的是,是否有任何解决方案或建议来显示选择内容? (示例项目链接在消息底部)

My problem is that programmatically setting the selectedRange of UITextView selects the text, but does not visually indicate it's selected. Does this sound like a bug in OS 3.0/3.1. More importantly, any solution or suggestions for getting the selection to display? (sample project linked at bottom of message)

由于我没有收到视觉反馈,我怎么知道它已被选中?两个原因:

  1. 没有闪烁的光标
  2. 点击键盘上的任意键会删除选择范围内的现有文本.

我在ViewController中有一个UITextView,它允许编辑用户可更改的值.当在表行上双击时打开编辑器控制器,或者用户单击行上的详细信息显示按钮. viewcontroller通过presentModalViewController到达,在编辑器VC的viewWillAppear方法中,我使用[self.textView becomeFirstResponder];

I have a UITextView in a viewcontroller which allows editing a user-changeable value. The editor controller is opened when a double-tap is received on a table row, or the user taps the detail disclosure button on a row. The viewcontroller arrives via presentModalViewController, and in the editor VC's viewWillAppear method, I initiate the keyboard with [self.textView becomeFirstResponder];

有时,我想在选择文本视图的全部内容的情况下启动编辑器,以便用户可以简单地开始键入内容并删除现有内容(这是因为我创建了具有默认标题的新项目,因此几乎可以确定用户是将首先删除编辑器文本字段的全部内容,然后键入其标题.

At times I want to start the editor with the entire contents of the textview selected so the user could simply start typing and it would delete the existing contents (this is because I create new items with default titles so it's almost certain the user will first delete the entire contents of the editor text field, then type their title).

我已经尝试过的一些事情:

  1. 在viewDidAppear中设置选定范围(而不是在textViewDidBeginEditing中进行设置).
  2. 用文本的副本替换_textView.text,然后应用selectedRange(在textViewDidBeginEditing中)...考虑一下,如果我正在处理框架错误,这可能会解决.
  3. [[_textView webView] selectAll];(专用api).同样的行为,文本被选择而没有视觉反馈.
  4. 发送_textView setNeedsDisplay和setNeedsLayout.
  5. 使用performSelector ... afterDelay设置selectedRange,使其在退出textViewDidBeginEditing后发生.
  1. setting the selected range in viewDidAppear (instead of and in addition to doing it in textViewDidBeginEditing).
  2. Replacing _textView.text with a copy of the text in it then applying the selectedRange (in textViewDidBeginEditing)... thinking maybe if I'm dealing with framework bug, this might get around it.
  3. [[_textView webView] selectAll]; (private api). same behavior, text gets selected with no visual feedback.
  4. sending _textView setNeedsDisplay and setNeedsLayout.
  5. using a performSelector ... afterDelay to set the selectedRange so that it happens after exiting textViewDidBeginEditing.

关于SO的引用:此处此处

// UITextFieldDelegate Methods...
- (void)textViewDidBeginEditing:(UITextView *)_textView
{
 NSInteger len = [_textView.text length];

 if( self.selectInitialText ){
  //NOTE: this is not working as expected. Indeed the text is 
  // selected but the highlighting of the text isn't showing.
  _textView.selectedRange = NSMakeRange(0,len);
 }
}

-编辑-
这是提交给Apple BugReporter的示例项目. 下载BugReport_7380723示例项目

-- EDIT --
Here's the sample project submitted to Apple BugReporter. Download BugReport_7380723 sample project

其他信息:

在创建和处理示例项目提交时,我发现了两件事:

While creating and playing with the sample project submission, I discovered two things:

  • 当还有的文本选择,但没有突出显示,轻按一些击键然后 摇撤消" 和原始文本被恢复的但IS 突出显示.

如果您在文本视图中手动进行选择,然后点击全选"按钮[指的是上面链接的示例项目],则所有文本均被选中并且会正确显示突出显示.

If you manually make a selection in the textview, then tap the "Select All" button [refers to the sample project linked above], all text is selected AND displays the highlight properly.

推荐答案

在以编程方式在textView上设置选择之前,只需调用select以使其在视觉上显示新选择.例如:

Before setting the selection programmatically on the textView, just call select to make it show the new selection visually. For example:

[textView select:self];
textView.selectedRange = newSelectedRange; 

这篇关于通过编程设置时不显示UITextView selectedRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆