NSSegmentedControl Colors

I'd like to have a subclass of NSSegmentedControl where the various segments are different colors. I've tried subclassing NSSegmentedControl and adding the following code:

- (void)drawRect:(NSRect)dirtyRect
{
    NSColor *color = [NSColor redColor];
    [color setFill];
    NSRectFill(dirtyRect);
    [super drawRect:dirtyRect];
}

That looks close except that 1. it colors the whole segmented control the same color, red in this case, and 2. there's a little bit of color bleed over on the edges.

I also tried subclassing NSSegmentedCell and adding this:

- (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)controlView
{
    NSColor *color;
    switch (segment) {
        case 0:
            color = [NSColor redColor];
            break;
        case 1:
            color = [NSColor blueColor];
            break;
        case 2:
            color = [NSColor greenColor];
            break;
        case 3:
            color = [NSColor orangeColor];
            break;
        default:
            break;
    }
    [color setFill];
    NSRectFill(frame);
    [super drawSegment:segment inFrame:frame withView:controlView];
}

This is better in that the various segments are displaying unique colors, however I would hardly consider this acceptable. I want the entire segment to be filled in with the appropriate color for that segment and it would be nice if it had the gradient and shading as in the first screenshot.

Please let me know how I can achieve this.

Thanks.


You need to specify fixed width for segments.

在这里输入图像描述

Then your code will work pretty good (Screenshot is from my test project)

And don't forget to disable tint

 [self.segment.cell setControlTint:NSClearControlTint ];
链接地址: http://www.djcxy.com/p/58666.html

上一篇: 合同究竟是什么?

下一篇: NSSegmentedControl颜色