UITextView selectedRange在编程设置时不显示 [英] UITextView selectedRange not displaying when set programatically

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

问题描述

我的问题是,通过程序设置UITextView的selectedRange 选择文本,但 不会 直观地指示它已被选中。这听起来像是OS 3.0 / 3.1中的错误。更重要的是,任何解决方案或建议获得选择显示? (示例项目链接在邮件底部)

My problem is that programatically 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)

我如何知道它是选择的,因为我没有得到视觉反馈?两个原因: / p>

How do I know it's selected since I'm not getting visual feedback? Two reasons:


  1. 不闪烁光标

  2. 点击键盘上的任何键可删除选择范围内的现有文字。 li>
  1. No blinking cursor
  2. Tapping any key on keyboard deletes existing text in the selection range.

我在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];

有时,我想启动编辑器,其中textview的所有内容选择,所以用户可以只是开始输入,它会删除现有的内容(这是因为我创建了新的项目与默认标题,所以几乎肯定用户将首先删除编辑器文本字段的全部内容,然后键入其标题)。

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]; (private 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);
 }
}

- EDIT -

下面是提交到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:


  • 如果选择了文字但未显示高亮显示,请点按一些按键,然后点击摇动即可撤消,并恢复原始文字 >高亮显示。

  • When there's text selected but no highlight displayed, tap some keystrokes then "shake to undo" and the original text is restored but IS highlighted.

如果在文本视图中手动选择,请点击选择全部按钮[指上面链接的示例项目]

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天全站免登陆