问题
I have a NSTextView on which I am appending console logs. I have implemented this UI in XIB file with scroll views. When my application appends the console logs in the text view, I want the scroll bar to automatically go to the end of the textview. I could not find any property in XIB editor. Any idea how can I achieve this?
@property(nonatomic, strong) IBOutlet NSTextView *logTextView;
[[self.logTextView textStorage] beginEditing];
[[[self.logTextView textStorage] mutableString] appendString:iData];
[[[self.logTextView textStorage] mutableString] appendString:@"\n"];
[[self.logTextView textStorage] endEditing];
回答1:
You can achieve your goal by using -[NSText scrollRangeToVisible:] after appending your string. (NSTextView is a subclass of NSText.)
Sorry, but that's the only way. As you observed, there's no IB check box that effects this particular behavior.
Apropos of nothing, you could replace your two -[NSMutableString appendString:] calls with one -[NSMutableString appendFormat:] call.
来源:https://stackoverflow.com/questions/16452110/auto-scrolling-to-the-end-of-nstextview