The problem is that the textStorage is not set up correctly on the NSTextView when you use Interface Builder in the way described.
I too wanted to use an NSTextView, without scrollView, in InterfaceBuilder. In Xcode 10 it doesn't seem possible to put a lone NSTextView in some custom view hierarchy (earlier answers have hinted that this was possible: https://stackoverflow.com/a/2398980/978300).
It is possible to get this working with the "CustomView" method in the question - however, this will only have the simple NSView properties in the Attributes inspector (i.e. you won't be able to customise font etc). You could pass some details through using @IBInspectable on your custom class.
The Connections Inspector seems to work correctly.
Example NSTextView subclass...
class MyTextView: NSTextView
{
// init(frame: sets up a default textStorage we want to mimic in init(coder:
init() {
super.init(frame: NSRect.zero)
configure()
}
/*
It is not possible to set up a lone NSTextView in Interface Builder, however you can set it up
as a CustomView if you are happy to have all your presentation properties initialised
programatically. This initialises an NSTextView as it would be with the default init...
*/
required init(coder: NSCoder) {
super.init(coder: coder)!
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
// By default, NSTextContainers do not track the bounds of the NSTextview
let textContainer = NSTextContainer(containerSize: CGSize.zero)
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
layoutManager.addTextContainer(textContainer)
replaceTextContainer(textContainer)
configure()
}
private func configure()
{
// Customise your text here...
}
}