2

我需要一个类似于NSLevelIndicatorin的控件NSContinuousCapacityLevelIndicatorStyle,但具有反向着色。使用NSLevelIndicator,颜色是这样的:绿色到警告级别,黄色从警告到严重级别,红色从严重级别开始。这适用于音量控制。但我有一个对应于油箱加注的值:我想要绿色表示满罐,黄色表示警告,红色表示空。我还没有找到任何改变颜色的方法NSLevelIndicator

那么,在我开始编写自己的自定义控件之前,是否有一个NSControl可用的地方已经可以满足我的需求(当然我在询问之前搜索过,但无济于事)?

谢谢阅读。

4

2 回答 2

1

子类化NSLevelIndicator并编写自己的- (void)drawRect:(NSRect)theRect

#import <Cocoa/Cocoa.h>


@interface PBLevelIndicator : NSLevelIndicator {

    unsigned int mPercent;
}


@end  

#import "PBLevelIndicator.h"


@implementation PBLevelIndicator
- (void)drawRect:(NSRect)theRect
{
    NSRect fillingRect = theRect;
    fillingRect.size.width = theRect.size.width*mPercent/100;   
    NSColor *indicatorColor;

    if( mPercent >= 99 )
    {
        indicatorColor = [NSColor greenColor];
    }
    else if (mPercent >50) 
    {
        indicatorColor = [NSColor yellowColor];
    }
    else
    {
        indicatorColor = [NSColor redColor];
    }
    [indicatorColor set];
    NSRectFill(fillingRect);
}

-(void) setPercentage:(unsigned int) inPercent
{
    mPercent = inPercent;
    [self setNeedsDisplay:YES];
}
@end
于 2013-02-06T11:25:04.400 回答
1

NSLevelIndicator(至少现在)如果您将警告级别设置为高于临界级别,则会自动为您执行此操作!

于 2016-09-02T06:44:02.000 回答