After typing anything in a custom text field with undo support, when opening the Edit menu, the undo item is disabled. It seems that setting translatesAutoresizingMaskIntoConstraints=false
, a formatter
, allowsEditingTextAttributes=true
and calling invalidateIntrinsicContentSize()
in textDidChange(_:)
causes this behaviour.
Is this expected, or is there a workaround?
Here is the code:
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var textField: NSTextField!
override func loadView() {
view = NSView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
let textField = MyTextField()
textField.frame = CGRect(x: 0, y: 0, width: 100, height: 20)
textField.translatesAutoresizingMaskIntoConstraints = false
textField.formatter = MyFormatter()
textField.allowsEditingTextAttributes = true
view.addSubview(textField)
}
}
class MyTextField: NSTextField {
override func textDidChange(_ notification: Notification) {
super.textDidChange(notification)
super.invalidateIntrinsicContentSize()
}
}
class MyFormatter: Formatter {
override func string(for obj: Any?) -> String? {
return obj as? String
}
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
obj?.pointee = string as NSString
return true
}
}