【问题标题】:Is there a custom control similar to NSLevelIndicator with reverse coloring?是否有类似于 NSLevelIndicator 的自定义控件,具有反向着色?
【发布时间】:2013-02-06 10:57:39
【问题描述】:

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

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

感谢阅读。

【问题讨论】:

    标签: objective-c macos cocoa nscontrol nslevelindicator


    【解决方案1】:

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

    【讨论】:

    • 就是这样!谢谢,我从来没有想过这个主意。
    【解决方案2】:

    子类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
    

    【讨论】:

    • 这是个好主意,谢谢。我试过了,但新类不符合键值编码。我会更深入地研究一下,看看我是否能解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多