I am making a an app that has a UITextView
and a button.
When I click the button some text will add in the UITextView
.
But when c
extension UITextView {
func simple_scrollToBottom() {
let textCount: Int = text.count
guard textCount >= 1 else { return }
scrollRangeToVisible(NSRange(location: textCount - 1, length: 1))
}
}
// Usage
textView.simple_scrollToBottom()
Make a range, specifying encoding, to the last character, then scroll to that range Something other than utf8 might be appropriate depending on your content
let range = NSMakeRange(self.OutputTextView.text.lengthOfBytes(using: String.Encoding.utf8), 0);
self.OutputTextView.scrollRangeToVisible(range);
Try this if you have problem on iOS 7 or above. See this SO answer.
- (void)scrollTextViewToBottom:(UITextView *)textView {
NSRange range = NSMakeRange(textView.text.length, 0);
[textView scrollRangeToVisible:range];
// an iOS bug, see https://stackoverflow.com/a/20989956/971070
[textView setScrollEnabled:NO];
[textView setScrollEnabled:YES];
}
This works for me! :D
CGPoint bottomOffset = CGPointMake(0, self.textView.contentSize.height - self.textView.bounds.size.height);
[self.description1 setContentOffset:bottomOffset animated:YES];