NSSegmentedControl颜色 [英] NSSegmentedControl Colors
问题描述
我想有一个NSSegmentedControl的子类,其中各个段是不同的颜色。我尝试子类化NSSegmentedControl并添加以下代码:
- (void)drawRect:(NSRect)dirtyRect
{
NSColor * color = [NSColor redColor];
[color setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
这看起来很接近,除了1.它使整个分段控件颜色相同,在这种情况下红色
我也试过对NSSegmentedCell进行子类化并添加:
- (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;
默认值:
break;
}
[color setFill];
NSRectFill(frame);
[super drawSegment:segment inFrame:frame withView:controlView];
}
这是更好的,因为各个段都显示独特的颜色,但我几乎不认为这是可以接受的。我想用整个段填充该段的适当颜色,如果它具有如第一个截图所示的渐变和阴影,那将是很好。
请让我知道如何实现这个。
谢谢。
您需要为细分指定固定宽度。
然后你的代码将工作得很好(截图来自我的测试项目)
不要忘记禁用着色
[self.segment.cell setControlTint:NSClearControlTint];
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 ];
这篇关于NSSegmentedControl颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!