如何将自定义NSFormatter设置为NSTextView? [英] How do I set a Custom NSFormatter to a NSTextView?
问题描述
我有一个NSTextView
,我有一个自定义的NSFormatter
,它限制了字符串的长度.但是我如何在NSTextView
上"[_textView setFormatter:customFormatter]
"?我找不到该怎么办.
示例代码将不胜感激!
I have a NSTextView
and i have a custom NSFormatter
that limits the string length. But how do i "[_textView setFormatter:customFormatter]
" on a NSTextView
? I can't find how to do that.
Example code would be appreciated!
推荐答案
无法将自定义NSFormatter与NSTextView一起使用. (这似乎是NSControl的省,其中NSTextField是其子类.)
It's not possible to use a custom NSFormatter with a NSTextView. (That appears to be the province of NSControl, of which NSTextField is a subclass.)
但是,可以为您的文本视图的文本存储(NSTextStorage
)设置一个委托,并实现-textStorageWillProcessEditing:
(一种NSTextStorageDelegate
的方法);在这里您可以限制用户的输入.您自定义的NSFormatter的现有代码可能会在此处进行修改.
However, it is possible to set up a delegate for your text view's text storage (NSTextStorage
), and implement -textStorageWillProcessEditing:
(a method of NSTextStorageDelegate
) ; this is where you could constrain your user's input. Your existing code for your custom NSFormatter might be adapted for use there.
这篇关于如何将自定义NSFormatter设置为NSTextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!