OK, what I need should have been very simple. However, I've looked everywhere and I'm not sure I've found something that works 100% (and it's something that has troubled me in the past too).
So, here we are :
NSTextView
NSTextView
should scroll down (so that that latest appended contents are visible)Rather straightforward, huh?
So... any ideas? (A code example that performs exactly this simple "trick" would be more than ideal...)
After cross-referencing several answers and sources (with some tweaks), here's the answer that does work (given _myTextView
is an NSTextView
outlet) :
- (void)appendToMyTextView:(NSString*)text { dispatch_async(dispatch_get_main_queue(), ^{ NSAttributedString* attr = [[NSAttributedString alloc] initWithString:text]; [[_myTextView textStorage] appendAttributedString:attr]; [_myTextView scrollRangeToVisible:NSMakeRange([[_myTextView string] length], 0)]; }); }
The appendAttributedString
and scrollToEndOfDocument
are available starting in OS X 10.0 and 10.6 respectively
extension NSTextView { func append(string: String) { self.textStorage?.appendAttributedString(NSAttributedString(string: string)) self.scrollToEndOfDocument(nil) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With