Question

I would like to individually set cell background colors in an NSForm. I thought it would be a matter of subclassing NSForm and overriding -drawCellAtRow:column:. But -drawCellAtRow:column: only gets called when the cell receives focus.

I've experimented with calling -setCellBackgroundColor on the NSForm but that changes the title background, not the value.

Is there an approach I'm not finding?

Was it helpful?

Solution

I think this just isn't possible with NSFormCell.

First thing I tried:

self.form.drawsCellBackground = YES;
self.form.cellBackgroundColor = [NSColor redColor];

This appears to draw the background only behind the label. Some further testing suggested that a single NSFormCell spans both the label and text field, and the background was drawing behind both. When the text field draws on top, its own background covers up the red color.

Next I subclassed NSFormCell. The only method to be called is -drawWithFrame:inView:. The frame, again, spans both the label and text field. The control view is the NSForm. If I draw my own background and then invoke [super drawWithFrame:inView:], it appears just as above, visible behind the label, but not visible behind the text field. If I invoke super and then draw the background, it appears atop both.

NSFormCell's implementation does not invoke -drawInteriorWithFrame:inView: nor does it seem to provide another override point.

I tried setting highlighted, and implementing -highlightColorWithFrame:inView: – same issue.

While I was trying to understand how this all worked I also tried subclassing NSForm. The only drawing-related method called is -drawRect:. These methods are never invoked: -drawCellAtIndex:, -drawCellAtRow:column:, -drawCell:, -drawCellInside:.

Apart from getting Apple to fix this, the only solutions I see are trying to use NSForm's superclass NSMatrix, or just using labels and text fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top