ColorGradientView.m (1220B)
1 // 2 // ColorGradientView.m 3 // uimacnew 4 // 5 // From http://www.katoemba.net/makesnosenseatall/2008/01/09/nsview-with-gradient-background/ 6 // 7 // 8 9 #import "ColorGradientView.h" 10 11 @implementation ColorGradientView 12 13 // Automatically create accessor methods 14 @synthesize startingColor; 15 @synthesize endingColor; 16 @synthesize angle; 17 18 - (id)initWithFrame:(NSRect)frame { 19 self = [super initWithFrame:frame]; 20 if (self) { 21 // Initialization code here. 22 [self setStartingColor:[NSColor gridColor]]; 23 [self setEndingColor:[NSColor controlShadowColor]]; 24 [self setAngle:270]; 25 } 26 return self; 27 } 28 29 - (void)drawRect:(NSRect)rect { 30 if (endingColor == nil || [startingColor isEqual:endingColor]) { 31 // Fill view with a standard background color 32 [startingColor set]; 33 NSRectFill(rect); 34 } 35 else { 36 // Fill view with a top-down gradient 37 // from startingColor to endingColor 38 NSGradient* aGradient = [[NSGradient alloc] 39 initWithStartingColor:startingColor 40 endingColor:endingColor]; 41 [aGradient drawInRect:[self bounds] angle:angle]; 42 [aGradient release]; 43 } 44 } 45 46 @end