NSTextView textDidChange/didChangeText not called for bindings

徘徊边缘 提交于 2019-12-22 09:05:33

问题


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.


回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!