Ohad Regev
Ohad Regev

Reputation: 5711

iOS >> UITextView >> Automatically "Jump" to the Last Line

I have a UITextView that automatically prints some sort of log whenever the user is doing something in a certain view.

I wish to have it "jump" to the last line whenever a new line is added. Currently it just stays where it is and you have to manually scroll down if you wish to see the new lines.

How do I do that?

Upvotes: 1

Views: 1228

Answers (3)

Nitin Alabur
Nitin Alabur

Reputation: 5812

haven't tried this, it should work

CGPoint bottomOffset = CGPointMake(0, theTextView.contentSize.height - theTextView.bounds.size.height);
[theTextView setContentOffset:bottomOffset animated:YES];

Upvotes: 1

Alessandro Vendruscolo
Alessandro Vendruscolo

Reputation: 14877

I haven't tried this, but according to the documentation there's - (void) scrollRangeToVisible:(NSRange)range.

So you would do this:

[myTextView scrollRangeToVisible:NSMakeRange([myTextView.text length], 0)]

Basically, we pass the past-the-bound range of your text.

Upvotes: 2

Wain
Wain

Reputation: 119031

UITextView is a subclass of UIScrollView. As such you can query its contentsSize and call setContentOffset:animated: to move to the new text.

Upvotes: 1

Related Questions