简体   繁体   中英

Scroll an NSTextView to bottom after adding text

I want to add text to an NSTextView and scroll it to the bottom like it is done in the console of Xcode. What is the best way to do that in swift 4? I have looked at other answers but they are not in swift or do not work perfectly in when converted to swift.

This is being written for an OS X app and not an iOS app and that is why I am asking about an NSTextView.

To add text, I am simply I am using this code self.logTextView.string += "\\nnew text" This allows the text to also be put on a new line. If there is a better way to do this, would love to hear it.

Here is what I have tried already: Scrolling NSTextView to bottom

NSTextView has an exact action: scrollToEndOfDocument(_ sender: Any?) .

So, I suppose the following code is enough.

self.logTextView.string += "\nnew text"
self.logTextView.scrollToEndOfDocument(nil)

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