Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView automatic scroll down

Tags:

cocoa-touch

I have an UIView and I add a editable UITextView to it,

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 219, 47)];
[self addSubview:textView];
textView.font = [UIFont systemFontOfSize:14];

When I type it doesn't go down automatic. Anybody got an idea?

like image 818
Andy Jacobs Avatar asked Feb 09 '09 14:02

Andy Jacobs


3 Answers

Just figured out that you must use a length > 0. Ie

NSRange range = NSMakeRange(textView.text.length - 1, 1);
[textView scrollRangeToVisible:range];

Worked for me anyway.

like image 62
Martin Wickman Avatar answered Oct 14 '22 18:10

Martin Wickman


To scroll to the end of the buffer I tried

[textView scrollRangeToVisible:NSMakeRange([textView.text length]-1, 1)];

But nothing happened. Instead this works

textView.selectedRange = NSMakeRange(textView.text.length - 1, 0);
like image 25
Frank Hintsch Avatar answered Oct 14 '22 18:10

Frank Hintsch


I think you'll have to do

[textView scrollRangeToVisible:[textView selectedRange]];

in textDidChange.

like image 34
Can Berk Güder Avatar answered Oct 14 '22 17:10

Can Berk Güder