NSTextView setFormatter?

903 Views Asked by At

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?

1

There are 1 best solutions below

0
Wain On BEST ANSWER

An NSFormatter is designed for the format of a single line string. This intention matches against NSTextField which 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 NSTextView to 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: and isPartialStringValid:newEditingString:errorDescription:).