With a NSTextField we can easially call setFormatter if we want to say for instance create a custom formatter that only allows numbers (take this question for example).
But what if we want to apply a formatter to a NSTextView? There does not seem to be a way to set a formatter, unless I am overlooking something.
Any suggestions on how to get around this?
An
NSFormatteris designed for the format of a single line string. This intention matches againstNSTextFieldwhich would usually show a single line (particularly if using a formatter).The point of a formatter is both to format existing text for display and to validate text entry from a user. You can use a formatter with an
NSTextViewto perform both of these actions, you just need to do a little work yourself to process text before you add it to the view (stringForObjectValue:) or when a user enters it (textView:shouldChangeTextInRange:replacementString:andisPartialStringValid:newEditingString:errorDescription:).