Reputation: 10251
I am trying a draw a circle with a background colour in my NSTableview's view based cell. I tried so many options none works fine.
I used a NSView subclass and clipped circular path in drawRect method. It worked, But whenever I scroll the table view it shape wasn't circular anymore.
CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:dirtyRect xRadius:10.0 yRadius:5.0];
[path addClip];
// Fill in background Color
const CGFloat *components = CGColorGetComponents(backgroundColor.CGColor);
CGContextSetRGBFillColor(context, components[0],components[1],components[2],components[3]);
CGContextFillRect(context, NSRectToCGRect(dirtyRect));
[super drawRect:dirtyRect];
I also tried using beveled buttons(rounded) but when I set the background colour to its cell it was not round. It became a square.
[result.tagImageButton.cell setBackgroundColor:[NSColor redColor]];
Upvotes: 0
Views: 1378
Reputation: 3018
Use [self bounds]
instead of dirtyRect
. As the scroll view scrolls, your cell may be asked to only draw parts of itself. In that case dirtyRect
is smaller than your cell. In that case it is OK though to still draw the entire cell.
Upvotes: 2