NSOutlineView with transparent field editor

I am working with NSOutlineView located in the HUD panel. I set it up so that it does not draw its background. Everything looks fine until I double-clicked to edit the cell. The field editor draws a background and focus ring that completely destroys the entire user interface.

This is what I do in a subclass of NSTextFieldCell:

- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj { NSText *text = [super setUpFieldEditorAttributes:textObj]; [text setDrawsBackground:YES]; [text setBackgroundColor:[NSColor darkGrayColor]]; return text; } 

If I use setDrawsBackground: NO, it is completely ignored and I get a white background. My solution is far from good, because I cannot touch the alpha component of the color (if I do this, the field editor will use a different color as the background), but at least I do not get a white background.

I am wondering if there is a real solution to this problem. Should I provide my own field editor? Is it worth it?

What I want is just a field editor without background and focus ring, only the cursor is blinking.

Thanks!

+4
source share
2 answers

The problem is that the white background is drawn by the NSTableView when it is sent -editColumn: row: withEvent: select :. It fills the cell rectangle + [NSColor textBackgroundColor].

If there is a public API for overriding the current parameter for named colors from the developerโ€™s color space, we can set it inside the -editColumn: row: withEvent: select: override or the like. I do not remember such an API (pointers are evaluated). SEE ALSO: I only tested this code on Snow Leopard (even the addition to the Leopard SDK below). Check the code for the actual SDKs and runtimes that you intend to support.

NSTableView has a private accessory that it uses for fill color, but this property is read-only. There is no setter, so we cannot just change the value on a standard NSTableView. We must subclass it. (Since you want the same behavior in outlineView and NSOutlineView to already be a subclass of NSTableView, we are going to subclass NSOutlineView. But, besides the superclass, the code is identical.)

 @interface ASCOutlineView : NSOutlineView { } @end @implementation ASCOutlineView - _textBackgroundColor { return ([NSColor clearColor]); } @end 

Everything seems to need to be prevented so that this bright white block destroys your HUD when editing table cells in Snow Leopard.

Applications compiled against the Leopard SDK need a little extra support. Leopard tableViews may have some hard-coded rendering properties, so we need to override the selection method.

NSTextFieldCells are actually wrappers for NSTextView, so they can be used inside controls. Usually they have the same textView instance controlled by the window (or its subclass, panel, in this case). NSTableView modifies the settings of NSTextFieldCell to match the system user interface settings for editing data. Mostly. NSTextFieldCell then passes these settings to NSTextView. At any point in this pipeline, we can override a method or two to change the values โ€‹โ€‹of these properties according to our own user interface.

I use - [NSTextFieldCell setDrawsBackground:], because it takes a little effort to fix it. It is also important to preserve the internal state in accordance with the effect that we hope to achieve if any other object may depend on this state.

 @interface ASCTextFieldCell : NSTextFieldCell { } @end @implementation ASCTextFieldCell - (void)setDrawsBackground: (BOOL)flag { [super setDrawsBackground: NO]; } @end 

And preventing the appearance of the focus ring while the cell being edited is just a matter of changing the setting of its type of focus ring. Frustratedly, IB does not provide access to this property, so it must be done programmatically:

 for(eachColumn in [hudOutlineView tableColumns]) { columnCell = [[ASCTextFieldCell alloc] initTextCell: @""]; [eachColumn setDataCell: columnCell]; if([columnCell respondsToSelector: @selector(setFocusRingType:)] != NO) [(NSTextFieldCell *)columnCell setFocusRingType: NSFocusRingTypeNone]; } 
+4
source

It looks like the field editor has a different background, which is drawn as white. Probably NSCell, or the background of the string, whatever.

0
source

Source: https://habr.com/ru/post/1345419/


All Articles