问题
I'm unable get to edit an nstextview. Here is the steps I tried.
- From the interface builder, I dragged dropped a "Custom view" into another view. (I couldnt find a nstextview there.)
- I changed the class of the Custom view form "NSView" to "NSTextView"
- Next I run my project, I can see the text view getting rendered (the mouse cursor changed to text-mode on the mouse-hover of text view area)
- However, I have not been able to insert/type/edit any text.
Note: I have tried setEditable, setSelectable & set firstResponder options recommended in other posts, which did not help.
回答1:
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...
}
}
来源:https://stackoverflow.com/questions/46349302/editable-nstextview-from-interface-builder