NSTextView 滚动

NSTextView Scrolling

如何使 NSTextView 可滚动?

问题:

问题:

代码:

class ViewController: NSViewController {

    private let textView                = NSTextView()

    //MARK: Manage Views

    override func viewDidLoad() {
        super.viewDidLoad()

        setupTextView()
    }


    private func setupTextView() {

        textView.isEditable      = false

        view.addSubview(textView)

        textView.translatesAutoresizingMaskIntoConstraints = false

        textView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        textView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        textView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        textView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }
}

NSTextView 不滚动,将文本视图放在 NSScrollView 内。 Class 方法 scrollableTextView() of NSTextView returns a NSTextView in a NSScrollView.

private var textView:NSTextView?

private func setupTextView() {

    let scrollView = NSTextView.scrollableTextView()
    textView = scrollView.documentView as? NSTextView

    textView?.isEditable      = false

    view.addSubview(scrollView)

    scrollView.translatesAutoresizingMaskIntoConstraints = false

    scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}