Search code examples
cocoacocoa-bindingsnstextview

NSTextView textDidChange/didChangeText not called for bindings


I have a custom NSTextView implementation that automatically adjusts the font size so that the text fills the entire view.

I overwrote didChangeText to call my font size adjustment method. Works great when the user is editing text, but didChangeText (and the delegate method textDidChange:) are not called when the text view contents are set via bindings.

The font adjustment code needs to run whenever the text is set/changes, not only when it's changed by the user.

How can I detect all changes to the text in an NSTextView, even via bindings?

Note: If there's a better way to have the text fill the entire text view other than increasing the font size, let me know.


Solution

  • It would be better to set the font attributes into the NSAttributedString that is bound to the text view's "attributedString". In the textDidChange: delegate method, you can just recreate the NSAttributedString with the correct font attributes.