Reputation: 1163
I'd like to do some type of refresh of a UITextView to set the textview back to it's original state. I have a paragraph that gets dynamically populated depending on which TableViewCell the user clicks on. So when they scroll the text field, then go back and select another cell and return, the text changes, but the scroll position remains as the user left it. How can I return it to its default state. Thanks!
Upvotes: 10
Views: 10864
Reputation: 511686
What finally worked for me what to clear the text content and then set it with the desired text on the next run loop.
// clear the text view
self.textView.text = ""
// set the text on the next run loop
dispatch_async(dispatch_get_main_queue()) {
self.textView.text = someText
}
Upvotes: 0
Reputation: 1195
[textview setScrollEnabled:YES];
textview.text = yourText;
[textview setScrollEnabled:NO];
Upvotes: 5
Reputation: 733
This works if you want it to scroll up to the top:
[theTextView scrollRangeToVisible:NSMakeRange(0, 0)];
The only problem is that this animates its way up. If you want it to snap to the top, use this:
[theTextView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
Upvotes: 6
Reputation: 7072
I managed to get the desired result - forcing the UITextView to 'scroll' to the top of its content when editing ends. But to achieve it I had to BOTH disable scrolling in textViewDidEndEditing AND ALSO reset the text in the same method.
I re-enable scrolling in textViewDidBeginEditing.
- (void) textViewDidEndEditing: textView;
{
[textView setScrollEnabled: NO];
NSString* savedText = [textView text];
[textView setText:@""];
[textView setText: savedText];
}
Upvotes: 1
Reputation: 19323
Neither one of these approaches worked satisfactorily for me.
scrollRangeToVisible produces tons of visual artifacts (lots of scrolling when switching between large strings), and in fact only works at all in a delayed call some time (I used 0.1 seconds) after the contents has been changed, not directly as Shaggy Frog's answer implies.
setting the text to @"" before setting the new contents didn't help, even with a delay.
It seems as if once a UITextView has been typed in, it forever is in this mode where setting new contents causes annoying scrolling artifacts. I tried setting the selectionRange to the beginning of the UITextView as well, but that didn't help. Sending the resignFirstResponder message before setting the new contents didn't help, either.
Because of this, I decided to delete and recreate the UITextView each time I change the contents. That's a rare enough event (based on human interaction) in my app that it's not a performance problem. It was the only way I could find to "load new contents" into the UITextView without tons of annoying scrolling artifacts.
My experience is with OS3.2 (ipad simulator)
Upvotes: 1
Reputation: 807
I've found that if I clear the UITextView first and then apply the new text, it will automatically "scroll" back to the top.
myTextView.text = @"";
myTextView.text = theRealTextContent;
Upvotes: 8
Reputation: 27601
By default state, do you mean scrolled to the top? If so, you're on the right track. Try
[myTextView scrollRangeToVisible:NSMakeRange(0, 0)];
Upvotes: 27