NSTextField usesSingleLineMode Stops Working When You Implement NSTextViewDelegate Methods
Today I learned why my NSTextField
permits pasting of newline characters even though I set usesSingleLineMode
properly. It's because I made it conform to NSTextViewDelegate
to cache changes. When you edit text inside of an NSTextField
, you actually type inside a field editor of the window. That's a shared NSTextView
instance. Most of the hard work of an NSTextField
is done by its cell
, which is an NSTextCell
. NSTextCell
s implement at least the delegate method NSTextViewDelegate.textView(_:shouldChangeTextIn:replacementText:)
– and when you set usesSingleLineMode
, this is actually set for the cell, not the view itself. You can use textView(_:shouldChangeTextIn:replacementText:)
to sanitize input text, and I suspect that's where the usesSingleLineMode
implementation happens. If your NSTextField
subclass implements this method, the NSTextCell
implementation isn't called. And since that one isn't public (it was called "implicit protocol conformance" back in the day), you cannot delegate up in Swift because the compiler knows it isn't there.