以编程方式设置时 UITextView selectedRange 不显示
我的问题是,以编程方式设置 UITextView 的 selectedRange 选择文本,但没有直观地表明它已被选中。这听起来像是 OS 3.0/3.1 中的错误吗?更重要的是,有什么解决方案或建议可以显示选择? (消息底部链接的示例项目)
由于我没有收到视觉反馈,我如何知道它已被选中? 两个原因:
- 光标不闪烁
- 点击键盘上的任意键会删除选择范围中的现有文本。
我在视图控制器中有一个 UITextView,它允许编辑用户可更改的值。当在表行上收到双击或用户点击行上的详细信息公开按钮时,编辑器控制器将打开。视图控制器通过presentModalViewController到达,在编辑器VC的viewWillAppear方法中,我用[self.textView成为FirstResponder];
启动键盘
有时我想启动编辑器并选择文本视图的全部内容,这样用户只需开始输入即可删除现有内容(这是因为我创建了具有默认标题的新项目,因此几乎可以肯定用户将首先删除编辑器文本字段的全部内容,然后输入其标题)。
我已经尝试过的一些事情:
- 在 viewDidAppear 中设置所选范围(而不是在 textViewDidBeginEditing 中进行设置)。
- 将 _textView.text 替换为其中文本的副本,然后应用 selectedRange (在 textViewDidBeginEditing 中)...想也许如果我正在处理框架错误,这可能会解决它。
[[_textView webView] selectAll];
(私有 API)。同样的行为,文本被选择而没有视觉反馈。- 发送 _textView setNeedsDisplay 和 setNeedsLayout。
- 使用 PerformSelector ... afterDelay 设置 selectedRange,以便在退出 textViewDidBeginEditing 后发生。
// 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 示例项目
其他信息:
创建时在处理示例项目提交时,我发现了两件事:
当选择了文本但没有突出显示时,点击一些按键,然后“摇动以撤消”,原始文本将恢复但突出显示.
如果您在文本视图中手动进行选择,然后点击“全选”按钮[指上面链接的示例项目],则所有文本都会被选中并且正确显示突出显示。
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)
How do I know it's selected since I'm not getting visual feedback? Two reasons:
- No blinking cursor
- Tapping any key on keyboard deletes existing text in the selection range.
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).
Some of the things I've tried already:
- setting the selected range in viewDidAppear (instead of and in addition to doing it in textViewDidBeginEditing).
- 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.
[[_textView webView] selectAll];
(private api). same behavior, text gets selected with no visual feedback.- sending _textView setNeedsDisplay and setNeedsLayout.
- using a performSelector ... afterDelay to set the selectedRange so that it happens after exiting textViewDidBeginEditing.
// 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 --
Here's the sample project submitted to Apple BugReporter. Download BugReport_7380723 sample project
Additional Information:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在以编程方式在 textView 上设置选择之前,只需调用 select 即可使其以可视方式显示新选择。例如:
Before setting the selection programmatically on the textView, just call select to make it show the new selection visually. For example:
我现在将回答我自己的问题,并在有更多信息时更新它(或者如果其他人找到合适的解决方法,我会很乐意将检查移至他们的答案)。我向苹果提交了一份错误报告,在提交示例代码后,它就处于开放状态。我非常确信这是一个错误,所以当我收到他们的回复时,我会在这里传递。
I'll answer my own question for now and update it later when there's more info (or if someone else finds a suitable work around, I'll happily move the check to their answer). I've got a bug report into apple and after submitting sample code, it's sitting there with open status. I'm pretty confident it's a bug, so when I get a response from them, I'll pass along here.
突然遇到这个问题。在我检查 NSObject 的声明标头之前,我遇到了同样的问题。他们已经提供了一个名为 selectAll 的函数:
Suddenly meet this question. I've had the same problem until I check NSObject's declare header. They 've already provided a function named selectAll:
解决方法:您似乎想要的是 UITextView 占位符功能。这当然不存在。不过,此处有一些解决方法
Workaround: What you seem to want is UITextView placeholder functionality. This of course does not exist. However, there are some workarounds here