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.
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.
The NSTextView method didChangeText is not called when a binding updates the text (as opposed to the text view updating the model).
didChangeText is the source of the binding update. If you override it and don't call super, the binding is broken. didChangeText calls the delegate method textDidChange.
Unfortunately, didChangeText is also called rather late in the NSTextView update process - after the layout and storage delegate calls.
I found that the NSTextStorageDelegate method "didProcessEditing" was the best way to catch changes to the bound string. Although you have to be careful what changes you can make back to the textview at this point - some calls crashed.
I answered my own similar question more fully here: NSTextView textDidChange not called through binding
来源:https://stackoverflow.com/questions/13875915/nstextview-textdidchange-didchangetext-not-called-for-bindings