I'm having difficult to set up NSScrollView for my text file display.

Defined a window with proper size:

......
    let contentRect = NSMakeRect(0.0, 0.0, 800, 600)
    let styleMask:NSWindow.StyleMask = [.titled, .closable, .miniaturizable, .resizable]
    let window = NSWindow(contentRect:contentRect, styleMask:styleMask, backing:.buffered, defer:true)
    window.minSize = NSMakeSize(800.0, 600.0)
    window.isReleasedWhenClosed = false
    window.tabbingMode = .disallowed
    window.makeKeyAndOrderFront(nil)
    window.center() // Wait until after makeKeyAndOrderFront so the window sizes properly first
    window.title = NSLocalizedString("GCode File", comment:"GCode File window")
......

set up scrollview and put it into windows and then set up window constraints:

......
    let scrollView = NSScrollView(frame: (window.contentView?.frame)!)
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.borderType = .noBorder
    scrollView.backgroundColor = NSColor.gray
    scrollView.hasVerticalScroller = true

    window.contentView?.addSubview(scrollView)
    window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: [], metrics: nil, views: ["scrollView": scrollView]))
    window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: [], metrics: nil, views: ["scrollView": scrollView]))

......

set up clipview and put clipview as scrollview's content view and then configure scrollview and clipview's constraints:

......
    let clipView = NSClipView()
    clipView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.contentView = clipView
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .left, relatedBy: .equal, toItem: scrollView, attribute: .left, multiplier: 1.0, constant: 0))
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1.0, constant: 0))
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .right, relatedBy: .equal, toItem: scrollView, attribute: .right, multiplier: 1.0, constant: 0))
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 0))
......

setup nstextview and load file content into nstextview:

......
    var textView: NSTextView!
    var textStorage: NSTextStorage!
    textStorage = NSTextStorage()
    let layoutManager = NSLayoutManager()
    textStorage.addLayoutManager(layoutManager)
    let textContainer = NSTextContainer(containerSize: window.contentView!.bounds.size)
    layoutManager.addTextContainer(textContainer)
    textView = NSTextView(frame: window.contentView!.bounds, textContainer: textContainer)
    textView.isEditable = true
    textView.isSelectable = true
    textView.textStorage?.append(file)
    scrollView.documentView = textView
......

Then I run the application, I got windows and content rendering up as:

enter image description here

What's wrong? I don't see the full text (truncated) that load from file and there is no vertical scroll bar shows as well. I believe there are some thing missed in my constraints configuration.

If any one have the experience, please advise!

0 Answers