Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, April 2, 2016

How do I force a UITextView to scroll to the top every time I change the text?

How do I force a UITextView to scroll to the top every time I change the text?


OK, I'm having some problem with the UITextView. Here's the issue:

I add some text to a UITextView. The user then double clicks to select something. I then change the text in the UITextView (programatically as above) and the UITextView scrolls to the bottom of the page where there is a cursor.

However, that is NOT where the user clicked. It ALWAYS scrolls to the bottom of the UITextView regardless of where the user clicked.

So here's my question: How do I force the UITextView to scroll to the top every time I change the text? I've tried contentOffset and scrollRangeToVisible. Neither work.

Any suggestions would be appreciated.

Answer by John Ballinger for How do I force a UITextView to scroll to the top every time I change the text?


Try this to move the cursor to the top of the text.

NSRange r  = {0,0};  [yourTextView setSelectedRange:r];  

See how that goes. Make sure you call this after all your events have fired or what ever you are doing is done.

Answer by Benjamin Sussman for How do I force a UITextView to scroll to the top every time I change the text?


I'm not sure if I understand your question, but are you trying to simply scroll the view to the top? If so you should do

[textview scrollRectToVisible:CGRectMake(0,0,1,1) animated:YES];

Answer by Wayne Lo for How do I force a UITextView to scroll to the top every time I change the text?


UITextView*note;  [note setContentOffset:CGPointZero animated:YES];  

This does it for me.

Answer by Patricia for How do I force a UITextView to scroll to the top every time I change the text?


[txtView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];  

This line of code works for me.

Answer by Maria for How do I force a UITextView to scroll to the top every time I change the text?


I was using attributedString in HTML with text view not editable. Setting the content offset did not work for me either. This worked for me: disable scroll enabled, set the text and then enable the scrolling again

[yourTextView setScrollEnabled:NO];  yourTextView.text = yourtext;  [yourTextView setScrollEnabled:YES];  

Answer by nerd2know for How do I force a UITextView to scroll to the top every time I change the text?


If anyone has this problem in iOS 8, I found that just setting the UITextView's text property, then calling scrollRangeToVisible with an NSRange with location:0, length:0, worked. My text view was not editable, and I tested both selectable and not selectable (neither setting affected the result). Here's a Swift example:

myTextView.text = "Text that is long enough to scroll"  myTextView.scrollRangeToVisible(NSRange(location:0, length:0))  

Answer by Géza Mikló for How do I force a UITextView to scroll to the top every time I change the text?


Combined the previous answers, now it should work:

talePageText.scrollEnabled = false  talePageText.textColor = UIColor.blackColor()  talePageText.font = UIFont(name: "Bradley Hand", size: 24.0)  talePageText.contentOffset = CGPointZero  talePageText.scrollRangeToVisible(NSRange(location:0, length:0))  talePageText.scrollEnabled = true  

Answer by RyanTCB for How do I force a UITextView to scroll to the top every time I change the text?


Calling

scrollRangeToVisible(NSMakeRange(0, 0))  

works but call it in

override func viewDidAppear(animated: Bool)  

Answer by shyla for How do I force a UITextView to scroll to the top every time I change the text?


[self.textView scrollRectToVisible:CGRectMake(0, 0, 320, self.textView.frame.size.height) animated:NO];  

Answer by miguel for How do I force a UITextView to scroll to the top every time I change the text?


And here is my solution...

    override func viewDidLoad() {          textView.scrollEnabled = false          textView.text = "your text"      }        override func viewDidAppear(animated: Bool) {          textView.scrollEnabled = true      }  

Answer by ageorgios for How do I force a UITextView to scroll to the top every time I change the text?


This is how it worked on iOS 9 Release so as the textView is scrolled on top before appearing on screen

- (void)viewDidLoad  {      [super viewDidLoad];      textView.scrollEnabled = NO;  }    - (void)viewWillAppear:(BOOL)animated  {      [super viewWillAppear:animated];      textView.scrollEnabled = YES;  }  

Answer by Keith MacInnis for How do I force a UITextView to scroll to the top every time I change the text?


I have an updated answer which will have the textview appear properly, and without the user experiencing a scroll animation.

override func viewWillAppear(animated: Bool) {      dispatch_async(dispatch_get_main_queue(), {          self.introductionText.scrollRangeToVisible(NSMakeRange(0, 0))      })  

Answer by Igor for How do I force a UITextView to scroll to the top every time I change the text?


For me fine works this code:

    textView.attributedText = newText //or textView.text = ...        //this part of code scrolls to top      textView.contentOffset.y = -64      textView.scrollEnabled = false      textView.layoutIfNeeded() //if don't work, try to delete this line      textView.scrollEnabled = true  

For scroll to exact position and show it on top of screen I use this code:

    var scrollToLocation = 50 //      textView.contentOffset.y = textView.contentSize.height      textView.scrollRangeToVisible(NSRange.init(location: scrollToLocation, length: 1))  

Setting contentOffset.y scrolls to the end of text, and then scrollRangeToVisible scrolls up to value of scrollToLocation. Thereby, needed position appears in first line of scrollView.

Answer by Hermann Klecker for How do I force a UITextView to scroll to the top every time I change the text?


This worked for me. It is based on RyanTCBs answer but it is the Objective-C variant of the same solution:

- (void) viewWillAppear:(BOOL)animated {      // For some reason the text view, which is a scroll view, did scroll to the end of the text which seems to hide the imprint etc at the beginning of the text.      // On some devices it is not obvious that there is more text when the user scrolls up.      // Therefore we need to scroll textView to the top.      [self.textView scrollRangeToVisible:NSMakeRange(0, 0)];  }  

Answer by Drew S. for How do I force a UITextView to scroll to the top every time I change the text?


Since none of these solutions worked for me and I wasted way too much time piecing together solutions, this finally solved it for me.

override func viewWillAppear(animated: Bool) {      dispatch_async(dispatch_get_main_queue(), {          let desiredOffset = CGPoint(x: 0, y: -self.textView.contentInset.top)          self.textView.setContentOffset(desiredOffset, animated: false)      })  }  

This is really silly that this is not default behavior for this control.

I hope this helps someone else out.

Answer by glace for How do I force a UITextView to scroll to the top every time I change the text?


Swift 2 Answer:

textView.scrollEnabled = false    /* Set the content of your textView here */    textView.scrollEnabled = true  

This prevents the textView from scrolling to the end of the text after setting it.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.