4

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.

4

2 回答 2

0

最好将字体属性设置NSAttributedString为绑定到文本视图的“attributedString”。在textDidChange:委托方法中,您可以NSAttributedString使用正确的字体属性重新创建。

于 2012-12-14T11:52:11.653 回答
0

当绑定更新文本(与更新模型的文本视图相反)时,不会调用 NSTextView 方法 didChangeText。

didChangeText 是绑定更新的来源。如果您覆盖它并且不调用 super,则绑定被破坏。didChangeText 调用委托方法 textDidChange。

不幸的是,在 NSTextView 更新过程中,didChangeText 也被调用得相当晚——在布局和存储委托调用之后。

我发现 NSTextStorageDelegate 方法“didProcessEditing”是捕获绑定字符串更改的最佳方法。尽管此时您必须小心您可以对 textview 进行哪些更改 - 一些调用崩溃了。

我在这里更全面地回答了我自己的类似问题: NSTextView textDidChange not called through binding

于 2019-02-11T08:43:31.263 回答