3

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?

Community
  • 1
  • 1
Kyle
  • 17,317
  • 32
  • 140
  • 246
  • 1
    Have a look into this, http://stackoverflow.com/questions/12919786/how-do-i-set-a-custom-nsformatter-to-a-nstextview – Dhanunjay Kumar Aug 28 '13 at 06:15
  • I'm missing something in this question or you have a typo. In the first paragraph you say "we can easily call", in the second you say "there doesn't not seem to be a way"... What is the problem? – James Webster Sep 01 '13 at 11:48
  • @JamesWebster my bad. Its `NSTextField` which `setFormatter` can be called on. Thanks for pointing that out! – Kyle Sep 01 '13 at 12:03

1 Answers1

2

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

Wain
  • 118,658
  • 15
  • 128
  • 151