NSScrollView усеченный текст и без полосы прокрутки - PullRequest
1 голос
/ 13 мая 2019

Мне сложно настроить NSScrollView для отображения моего текстового файла.

Определено окно с правильным размером:

......
    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")
......

настроить scrollview и поместить его в окна, а затем установить ограничения окна:

......
    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]))

......

настроить просмотр клипа и поместить просмотр клипа в качестве просмотра содержимого scrollview, а затем настроить ограничения scrollview и clipview:

......
    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))
......

настроить nstextview и загрузить содержимое файла в 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
......

Затем я запускаю приложение, я получаю окна и контент рендеринга как:

enter image description here

Что не так? Я не вижу полный текст (усеченный), который загружается из файла, а также нет вертикальной полосы прокрутки. Я полагаю, что что-то пропущено в моей конфигурации ограничений.

Если у кого-то есть опыт, пожалуйста, сообщите!

...