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)
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.