<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Makes No Sense At All &#187; NSView</title>
	<atom:link href="http://www.katoemba.net/makesnosenseatall/tag/nsview/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.katoemba.net/makesnosenseatall</link>
	<description>Interesting things to share around software development, music and what else comes to mind</description>
	<lastBuildDate>Mon, 17 May 2010 19:59:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NSView with gradient background</title>
		<link>http://www.katoemba.net/makesnosenseatall/2008/01/09/nsview-with-gradient-background/</link>
		<comments>http://www.katoemba.net/makesnosenseatall/2008/01/09/nsview-with-gradient-background/#comments</comments>
		<pubDate>Wed, 09 Jan 2008 21:27:41 +0000</pubDate>
		<dc:creator>Berrie</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Colored view]]></category>
		<category><![CDATA[Mac OS X 10.5]]></category>
		<category><![CDATA[NSGradient]]></category>
		<category><![CDATA[NSView]]></category>

		<guid isPermaLink="false">http://www.katoemba.net/makesnosenseatall/index.php/2008/01/09/nsview-with-gradient-background/</guid>
		<description><![CDATA[In Mac OS X 10.5 a new class NSGradient is introduced, that makes it really easy to work with gradients (as you might have guessed from the name). The sample code below shows a basic yet functional implementation of an NSView subclass that has either an colored or gradient background.
ColorGradientView.h
@interface ColorGradientView : NSView
{
  NSColor [...]]]></description>
			<content:encoded><![CDATA[<p>In Mac OS X 10.5 a new class <a href="http://developer.apple.com/documentation/Cocoa/Reference/NSGradient_class/Reference/Reference.html" target="_blank">NSGradient</a> is introduced, that makes it really easy to work with gradients (as you might have guessed from the name). The sample code below shows a basic yet functional implementation of an <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html" target="_blank">NSView</a> subclass that has either an colored or gradient background.</p>
<p>ColorGradientView.h</p>
<pre><code>@interface ColorGradientView : NSView
{
  NSColor *startingColor;
  NSColor *endingColor;
  int angle;
}

// Define the variables as properties
@property(nonatomic, retain) NSColor *startingColor;
@property(nonatomic, retain) NSColor *endingColor;
@property(assign) int angle;

@end
</code></pre>
<p>ColorGradientView.m</p>
<pre><code>#import "GradientView.h"
@implementation ColorGradientView

// Automatically create accessor methods
@synthesize startingColor;
@synthesize endingColor;
@synthesize angle;

- (id)initWithFrame:(NSRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code here.
    [self setStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
    [self setEndingColor:nil];
    [self setAngle:270];
  }
  return self;
}

- (void)drawRect:(NSRect)rect {
  if (endingColor == nil || [startingColor isEqual:endingColor]) {
    // Fill view with a standard background color
    [startingColor set];
    NSRectFill(rect);
  }
  else {
    // Fill view with a top-down gradient
    // from startingColor to endingColor
    NSGradient* aGradient = [[NSGradient alloc]
        initWithStartingColor:startingColor
        endingColor:endingColor];
    [aGradient drawInRect:[self bounds] angle:angle];
  }
}

@end
</code></pre>
<p>Note that the code is based on the new garbage collection mechanism available in Objective-C 2.0, so there are no retain or release calls.</p>
<p>In the following picture you see a gradient effect from top to bottom to give a subtle 3d appearance:</p>
<p><img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/01/gradient-screenshot.png" alt="" width="566" height="26" /></p>
<p>To achieve this, add a <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html" target="_blank">NSView</a> in Interface Builder and define its class as ColorGradientView. In your controller, add a ColorGradientView outlet (e.g. myGradientView) and in Interface Builder attach the view to the outlet. Add the following 3 lines to the <code>- (void)awakeFromNib</code> method in the controller:</p>
<pre><code>
[myGradientView setStartingColor:
    [NSColor colorWithCalibratedWhite:0.85 alpha:1.0]];
[myGradientView setEndingColor:
    [NSColor colorWithCalibratedWhite:0.7 alpha:1.0]];
[myGradientView setAngle:270];
</code></pre>
<p>In upcoming post I will go into details of how to create the Safari like buttons that you see in the screen shot.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.katoemba.net/makesnosenseatall/2008/01/09/nsview-with-gradient-background/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
