Question

I'm trying to draw a transparent highlight color in my TableView pretty simply:

if ([self isSelected]){
    self.alphaValue = 0.5f;
    NSColor * fillColor = [NSColor colorWithCalibratedRed: 100.0f/256.0f 
                                                    green: 100.0f/256.0f 
                                                     blue: 100.0f/256.0f 
                                                    alpha: 0.5];


    [fillColor drawSwatchInRect:dirtyRect];

}

It draws something a bit odd though, a digonal line between the original color and the new alpha color. I know the solution is probably something pretty simple-- but I can't seem to figure out what it is. Any ideas?

Was it helpful?

Solution

I think this is just what drawSwatchInRect: is designed to do. When you have any alpha value, the swatch shows how your color looks when composited with black (upper left) and white (lower right). Wherever Apple UI components display a color, they do this. It's more obvious if the color you're using is not a shade of gray.

Color swatches with varying opacity

If you want to fill a rectangle with a specific color, letting the background show through, try this:

[fillColor set];
NSBezierPath *rectPath = [NSBezierPath bezierPathWithRect:dirtyRect];
[rectPath fill];

While this will work for filled rects, note that dirtyRect is not always the same shape. For most drawing, you want to use the bounds of the view being drawn.

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