DBBackgroundView 1.0

Tuesday, January 16, 2007

Last week I described the many ways you can add backgrounds to a window in Cocoa. Today I've got a class that will simplify the process for you. Just change any view in your project to the DBBackgroundView class (an NSView subclass), and let it draw colors, patterns, gradients, or images behind other objects in the window.



Download: DBBackgroundView 1.0

The download contains a full sample project for you to try. All you need to move into your project is the DBBackgroundView.m and .h files. Then change any NSView (even the window content view) to a DBBackgroundView and send it one or more of the messages listed below to configure the background.

- (void)clearBackground;
Clears any background settings and restores the default (no background) settings.

- (void)setBackgroundAlpha:(float)anAlpha;
Sets the transparency of the background color, pattern, or gradient. Pass a value from 0.0 (transparent) to 1.0 (opaque).

- (void)setBackgroundColor:(NSColor *)aColor;
Sets the background to a single color.

- (void)setBackgroundCornerRadius:(float)aRadius;
Use this to round the corners of the background. Pass a value from 0.0 (square corners) to up to half of the view's shortest side. The corner radius is used for all backgrounds, even when drawing a single image in the background.

- (void)setBackgroundGradient:(id)gradient;
Pass a CTGradient object to use as the background. The gradient will be drawn horizontally (at a 90° angle).

- (void)setBackgroundGradient:(id)aGradient withAngle:(float)anAngle;
Draws the CTGradient using the specified angle (0.0 to 360.0).

- (void)setBackgroundImage:(NSImage *)anImage;
Sets an image to draw centered in the background. The image can be drawn by itself, or combined with a color, pattern, or gradient background.

- (void)setBackgroundImage:(NSImage *)anImage withAlpha:(float)anAlpha;
Sets the image to draw centered in the background using the specified transparency (0.0 to 1.0).

- (void)setBackgroundPattern:(NSImage *)anImage;
Sets an image to be tiled throughout the background.

0 Comments