2013-12-20 108 views
1

我想爲我的NSLevelIndicator創建一個自定義類,它只是將邊緣四捨五入。 (即像圓角的矩形按鈕) 我創建了一個自定義類,我知道我必須在使NSLevelIndicator繪製圓角邊緣

-(void)drawRect:(NSRect)dirtyRect; 

方法來實現這一點,但我完全不知道如何實現這一點,我希望有人有一個想法這個。

+1

「...我知道我必須在['drawRect:']方法中實現這個...」這裏有一個問題:NSLevelIndicator是一個控件,而不是常規視圖,所以您必須至少部分在單元級。好消息是,你可能根本不需要繼承NSLevelIndicator;壞消息是,你不得不繼承NSLevelIndicatorCell的子類(如果沒有的話)。 –

回答

1

Output

@interface DILevelIndicatorCell : NSLevelIndicatorCell 

@property (readwrite, assign) BOOL drawBezel; 

@property (readwrite, assign) BOOL verticalCenter; 

@end 

- (void)drawWithFrame:(NSRect) rcCellFrame inView:(NSView*) pControlView 
{ 

    NSRect rcInterior = rcCellFrame; 

    if(_drawBezel) 
    { 
     [ self _drawBezelWithFrame:rcCellFrame ]; 

     rcInterior = NSInsetRect(rcCellFrame, 3, 3); 
    } 

    if(_verticalCenter) 
    { 
     CGFloat i = (NSHeight(rcInterior) - [ self cellSize ].height)/2; 
     rcInterior.origin.y += i; 
     rcInterior.size.height -= i; 
    }  

    [ super drawWithFrame:rcInterior inView:pControlView ]; 
} 

- (void)_drawBezelWithFrame:(NSRect) dirtyRect 
{ 

    CGFloat fRoundedRadius = 10.0f; 

    NSGraphicsContext* ctx = [ NSGraphicsContext currentContext ]; 
    [ ctx saveGraphicsState ]; 
    { 
     NSBezierPath* pPath = [ NSBezierPath bezierPathWithRoundedRect:dirtyRect 
                    xRadius:fRoundedRadius 
                    yRadius:fRoundedRadius ]; 
     [ pPath setClip ]; 

     [ [ NSColor colorWithCalibratedRed:0.9 green:0.9 blue:0.95 alpha:1.0 ] setFill ]; 
     NSRectFillUsingOperation (dirtyRect, NSCompositeSourceOver); 

     [ [ NSColor colorWithCalibratedRed:0.7 green:0.7 blue:0.85 alpha:1.0 ] setFill ]; 
     [ pPath setLineWidth:1 ]; 
     [ pPath stroke ]; 
    } 
    [ ctx restoreGraphicsState ]; 
} 
+0

謝謝,但不是我正在尋找的那種風格。它似乎只有一條線圍繞着LevelIndicator繪製。我試圖改變綠色的電平指示器 – user3124229

0

子類NSLevelIndicatorCell並覆蓋drawWithFrame:inView:

#import "CustomLevelIndicatorCell.h" 

@implementation CustomLevelIndicatorCell 

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 

    double level = (self.floatValue - self.minValue)/(self.maxValue- self.minValue); 
    if (level > 1.0){level = 1.0;} 
    NSLog(@"Level: %a", level); 

    NSColor *fillColor; 
    if(self.value < self.criticalValue) 
     fillColor = [NSColor redColor]; 
    else if(self.value < self.warningValue) 
     fillColor = [NSColor yellowColor]; 
    else 
     fillColor = [NSColor greenColor]; 


    NSRect levelRect = NSInsetRect(cellFrame, 2, 1); 
    levelRect.size.width = levelRect.size.width * level; 
    NSBezierPath * levelPath = [NSBezierPath bezierPathWithRoundedRect:levelRect xRadius:3 yRadius:3]; 
    [fillColor setFill]; 
    [levelPath fill]; 
    NSBezierPath * indicatorPath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(cellFrame, 2, 1) xRadius:3 yRadius:3]; 
    [indicatorPath setLineWidth:1]; 
    [[NSColor grayColor] setStroke]; 
    [indicatorPath stroke]; 

} 

@end 

您可以NSLevelIndicator的單元格,然後正好被設置爲你的CustomLevelIndicatorCell,無論是在InterfaceBuilder中或代碼通過setCell:

希望這有助於!