How do I set a Custom NSFormatter to a NSTextView?

2019-08-24 17:40发布

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!

1条回答
我命由我不由天
2楼-- · 2019-08-24 18:07

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.)

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.

查看更多
登录 后发表回答