duan
duan

Reputation: 8885

How to disable user interaction of NSTextView?

In Cocoa Touch, you can disable user interaction of a text view like this:

let textView = UITextView()
textView.isUserInteractionEnabled = false

But in Cocoa, NSTextView has no such easy API to use.
So what's the proper way to disable NSTextView user interaction?

Upvotes: 2

Views: 1970

Answers (1)

duan
duan

Reputation: 8885

You can disable its' editable by:

textView.isEditable = false

And its' selectable by:

textView.isSelectable = false

However, when you disable both of them. This textView still will intercept Mouse-down event, which is different from isUserInteractionEnabled .

So, for now, to enable click-through, my solution is subclassing a NSTextView and override its' hitTest:

class TextView: NSTextView {
  override func hitTest(_ point: NSPoint) -> NSView? {
    return nil
  }
}

An encapsulated way is here

class StaticTextView: NSTextView {
  override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)
  }

  override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
    super.init(frame: frameRect, textContainer: container)
  }

  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  override func hitTest(_ point: NSPoint) -> NSView? {
    return nil
  }
}

Upvotes: 6

Related Questions