views:

258

answers:

2

What's the minimum implementation needed to make a custom NSView with an editable text area? I assume NSTextFieldCell can be used for this. I've succeeded in drawing the cell in the view (which is straightforward), but making it editable seems to require a more complicated coordination between the view and the cell. Is there sample code available somewhere?

Update. I should have made clear that my longer-term goal is to have many more editable text areas on the same view. AFAIU it is better to use cells in that case as they are more light-weight than full-blown views. My updated question is: What's the minimum implementation needed to make a custom NSView with an editable text area using an appropriate NSCell?

+1  A: 

What's the minimum implementation needed to make a custom NSView with an editable text area?

  1. Make an NSView.
  2. Put an NSTextField in it.

Remember, NSViews (custom or otherwise) can contain other NSViews, and an NSTextField is a kind of NSView.

If you don't want code outside the custom view class to know about the text field, and it probably shouldn't, the custom view can create the text field and add it to itself as a private implementation detail. To do this, simply don't expose the text field in the custom view class's @interface (aside from the instance variable declaration, which is unavoidable).

The custom view should, of course, not draw wherever it put its text field. It could draw there, but the text field would cover it.

I assume NSTextFieldCell can be used for this.

Yes, if you don't mind reimplementing NSTextField. Adding an NSTextField as a subview of your view is much easier.

Peter Hosey
Thanks for the answer! You make a good point that using an NSTextField is possible and would be easier. But I think I'd better use cells once I add more editable text areas to the custom view. I've updated the question to reflect this.
Rinzwind
Rinzwind: I still don't see why you want to use cells for this. You can have multiple subviews.
Peter Hosey
I realize you can also have more than one subview. I do appreciate your suggestion to "backtrack" a little and question whether using cells directly is really the way to go, but let's take that as a given for the moment. I would still like to know how to coordinate between the custom NSView and the appropriate standard NSCell subclass for text editing. I haven't found a clear explanation of this in the Apple docs yet. There's a sample project on how to make a custom view and custom cell displaying a clock face, but AFAIK it doesn't do any text editing (+it requires 10.6 which I don't have)
Rinzwind
The relevant documentation is Control and Cell Programming Topics: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ControlCell/ The reason why there's nothing (or very little, if there's any) about hosting an NSTextFieldCell inside a custom view is that it is, at best, a very uncommon task. The NSTextField, NSTableView with an NSTextFieldCell, and NSMatrix with NSTextFieldCells solutions cover 99% of cases.
Peter Hosey
A: 

If you want to make a grid of text fields (with a dynamic number of them, perhaps), use an NSMatrix of NSTextFieldCells. You can, of course, add the NSMatrix as a subview of your custom view.

Peter Hosey