4

I have a CoreData object, and I want to display information from that object in a NSTextField.

The object has two relevant properties:

@property (nonatomic, retain) NSString *text;
@property (nonatomic, readonly) NSAttributedString *displayText;

I would like to display the latter in the text field, but allow the user to edit the former. (The latter string is a simple transformation of the former, with some characters removed and with some attributes applied.)

How can I achieve this?


There are two more points worth noting:

  • the NSTextField is inside a NSOutlineView, which means that the delegate methods control:textShould{Begin,End}Editing: are not called correctly: see this question. This means that I can't manually set the .attributedStringValue of the field before and after editing.

  • As my object is a subclass of NSManagedObject, I cannot use it alongside a NSFormatter via setObjectValue: (as it does not implement <NSCopying>). I thus can't seem to make the approach in this question work.


I'm open to either using an NSFormatter subclass, or (preferably) finding a way to bind the text displayed normally and while editing to the properties directly.

My outline view is view-based, so if there's a better fit for the job than NSTextField I am happy to use it instead.

Community
  • 1
  • 1
sapi
  • 9,944
  • 8
  • 41
  • 71
  • Were you able to achieve this? I've got the same question. I'd like my NSTextField to display a different value but edit a slightly modified string – strangetimes Oct 16 '18 at 23:58

0 Answers0