不为绑定调用NSTextView textDidChange / didChangeText [英] NSTextView textDidChange/didChangeText not called for bindings

查看:73
本文介绍了不为绑定调用NSTextView textDidChange / didChangeText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 NSTextView 实现,该实现会自动调整字体大小,以使文本填充整个视图。

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

我重写了 didChangeText 来调用我的字体大小调整方法。当用户正在编辑文本时,效果很好,但是在调用文本时不调用 didChangeText (和委托方法 textDidChange:)。文本视图的内容是通过绑定设置的。

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.

如何通过绑定来检测 NSTextView 中文本的所有更改?

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.

推荐答案

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

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.

这篇关于不为绑定调用NSTextView textDidChange / didChangeText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆