简体   繁体   中英

Auto scrolling to the end of NSTextView

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];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM