1

I'm programming an OS X application in XCode using Swift 3 and the storyboard functionality.

I have text that gets loaded dynamically (size is not pre-defined) in a NSTextView inside a NSClipView inside a NSScrollView. The problem is that this text gets clipped when it's bigger than the size of the NSClipView and no scrollbar appears. What am I missing?

storyboard

The storyboard with constraints. The "No Lyrics" cell defines the NSTextView where the text will be loaded into.

@IBOutlet weak var lyricsTextField: NSTextField!
@IBOutlet weak var lyricsClipView: NSClipView!
@IBOutlet weak var lyricsView: NSView!
@IBOutlet weak var lyricsScrollView: NSScrollView!

// This method gets called every time the string value changes
func updateForTrack() {
    lyricsTextField.stringValue = trackManager.currentLyrics

    // Some things I tried without any success
    lyricsTextField.sizeToFit()
    print(lyricsTextField.frame.size) // the size is correct (not clipped)
    lyricsClipView.documentView?.setFrameSize(lyricsTextField.frame.size)
    lyricsScrollView.documentView?.setFrameSize(lyricsTextField.frame.size)
}

And the result:

result

The view is not scrollable.

I would appreciate any hint in the right direction.

Isa
  • 1,121
  • 3
  • 10
  • 17
  • Possible duplicate of [How to resize NSTextView according to its content?](http://stackoverflow.com/questions/2654580/how-to-resize-nstextview-according-to-its-content) – Willeke Oct 13 '16 at 23:50
  • I've read that question but it doesn't help me as it is Objective C instead of Swift and the proposed solution (resizing the TextView) is already done in my case. – Isa Oct 16 '16 at 11:20
  • There is no Objective-C in the answer. The answer describes how to get the correct size for the scroll view. The text view is resized automatically. Don't call `sizeToFit`, it will change the width. – Willeke Oct 16 '16 at 13:56
  • 1
    `lyricsTextField` is a `NSTextField`, not `NSTextView`. Replace the scroll view and its contents by a `NSTextView` in IB. – Willeke Oct 16 '16 at 14:06
  • There is also a NSView (lyricsView here) in between the NSTextField and the scroll view. I'll try to replace the scroll view but I don't see how that helps as I still want to keep the scrolling functionality. – Isa Oct 16 '16 at 15:23
  • 1
    `NSTextView` in IB comes with a scroll view. – Willeke Oct 16 '16 at 15:35
  • Thank you, replacing the NSScrollView with a NSTextView did the trick. – Isa Oct 16 '16 at 15:50
  • @Isa Can you post your answer on how you got this to work with the storyboard? – Clifton Labrum Feb 06 '19 at 20:15

0 Answers0