1

I'm unable get to edit an nstextview. Here is the steps I tried.

  1. From the interface builder, I dragged dropped a "Custom view" into another view. (I couldnt find a nstextview there.)
  2. I changed the class of the Custom view form "NSView" to "NSTextView"
  3. 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)
  4. 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.

Arjun
  • 379
  • 2
  • 12
  • NSTextView is present in IB. It is called "Text View". Type "NSTextView" in the search field. – Willeke Sep 21 '17 at 22:07
  • @willeke : yeah I saw that, however on inspecting the class names, I found that it is a group of views including a nsscrollview, scroller, nsclipview and the nstextview. So, can't we have the nstextview as a stand-alone instead of having these other views ? – Arjun Sep 22 '17 at 06:17
  • No, there isn't a text view without scroll view in IB. Maybe NSTextView isn't supposed to be used without a scroll view. Can't you use a wrapping text field? Is the text view in a popover or a split view? – Willeke Sep 22 '17 at 10:05
  • @Willeke, I'm using the one with the scroll view with the scroll vertical and horizontal disabled to move forward. However, still not sure why NSTextView as a standalone doesnot work(or should not work) when I tried the steps in the question. – Arjun Sep 22 '17 at 12:11
  • I think this is because testStorage isn't set-up when you do things the custom-view way. Setting that up in a derived NSTextView class may be a way forward. – Giles Jul 02 '19 at 08:30

1 Answers1

3

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...
    }
}
Giles
  • 1,428
  • 11
  • 21