How do I set a Custom NSFormatter to a NSTextView?

2019-08-24 18:10发布

问题:

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:

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.