0
votes

I created a custom NSTextFieldCell and overwrote - (void)drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView to do my own drawing here. However, I have trouble with background drawing. Without calling super the background is not cleared and subsequent drawings create something like a smear effect. This does not happen when drawsBackground is set, as in this case I can just fill the cellFrame with the background color.

- (void)drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView {
    if (self.drawsBackground) {
        [self.backgroundColor set];
    } else {
        [NSColor.clearColor set];
    }
    NSRectFill(cellFrame);

    [self.attributedStringValue drawInRect: cellFrame];
}

enter image description here

But what do I have to do to clear the background in case background drawing is disabled? I want to let the other content under the text view to shine through of course (so, just erasing with the superview's background color is no solution).

1

1 Answers

2
votes

If you try to fill the cell with a [NSColor clearColor] it will draw it Black. Try to avoid the Fill when it is not needed. And you will be able to remove your super call.

Example:

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    if (self.drawsBackground) {
        if (self.backgroundColor && self.backgroundColor.alphaComponent>0) {

            [self.backgroundColor set];
            NSRectFill(cellFrame);
        }
    }
    NSRect titleRect = [self titleRectForBounds:cellFrame];
    NSAttributedString *aTitle = [self attributedStringValue];
    if ([aTitle length] > 0) {
        [aTitle drawInRect:titleRect];
    }
}