<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3" -->
<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/"
	>

<channel>
	<title>Paste Buffer</title>
	<link>http://www.pastebuffer.com</link>
	<description>Code snippets and examples - by Matt Legend Gemmell</description>
	<pubDate>Mon, 12 May 2008 11:10:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3</generator>
	<language>en</language>
			<item>
		<title>Constructing an NSColor from Color panel values</title>
		<link>http://www.pastebuffer.com/2007/10/05/constructing-an-nscolor-from-color-panel-values/</link>
		<comments>http://www.pastebuffer.com/2007/10/05/constructing-an-nscolor-from-color-panel-values/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 17:10:08 +0000</pubDate>
		<dc:creator>Matt Legend Gemmell</dc:creator>
		
		<category><![CDATA[Cocoa]]></category>

		<category><![CDATA[255]]></category>

		<category><![CDATA[color]]></category>

		<category><![CDATA[constructor]]></category>

		<category><![CDATA[NSColor]]></category>

		<category><![CDATA[panel]]></category>

		<category><![CDATA[values]]></category>

		<guid isPermaLink="false">http://www.pastebuffer.com/2007/10/05/constructing-an-nscolor-from-color-panel-values/</guid>
		<description><![CDATA[I often find myself constructing an NSColor object based on having sampled a color using the standard Color panel. The issue, of course, is that you need to divide the values in the picker by 255 to get the respective red/green/blue components for an RGB NSColor, as those constructors take values in the 0-1 range.
Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I often find myself constructing an NSColor object based on having sampled a color using the standard Color panel. The issue, of course, is that you need to divide the values in the picker by 255 to get the respective red/green/blue components for an RGB NSColor, as those constructors take values in the 0-1 range.</p>
<p>Here&#8217;s a trivial category on NSColor to allow you to use the values straight out of the Color panel.</p>
<p>Header:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co2">#import &lt;Cocoa/Cocoa.h&gt;</span></p>
<p><span class="kw4">@interface</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> <span class="br0">&#40;</span>PickerValues<span class="br0">&#41;</span></p>
<p>+ <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> *<span class="br0">&#41;</span>colorWithCalibratedPickerRed:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>red green:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>green <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blue:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>blue alpha:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>alpha;</p>
<p><span class="kw4">@end</span></div>
<p>Implementation:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co2">#import &quot;NSColor+PickerValues.h&quot;</span></p>
<p><span class="kw4">@implementation</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> <span class="br0">&#40;</span>PickerValues<span class="br0">&#41;</span></p>
<p>+ <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> *<span class="br0">&#41;</span>colorWithCalibratedPickerRed:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>red green:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>green <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blue:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>blue alpha:<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>alpha<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">float</span> maxVal = <span class="nu0">255.0</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> colorWithCalibratedRed:<span class="br0">&#40;</span>red / maxVal<span class="br0">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;green:<span class="br0">&#40;</span>green / maxVal<span class="br0">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blue:<span class="br0">&#40;</span>blue / maxVal<span class="br0">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alpha:alpha<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">@end</span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.pastebuffer.com/2007/10/05/constructing-an-nscolor-from-color-panel-values/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Setting the text color of an NSButton</title>
		<link>http://www.pastebuffer.com/2007/10/04/setting-the-text-color-of-an-nsbutton/</link>
		<comments>http://www.pastebuffer.com/2007/10/04/setting-the-text-color-of-an-nsbutton/#comments</comments>
		<pubDate>Thu, 04 Oct 2007 20:56:30 +0000</pubDate>
		<dc:creator>Matt Legend Gemmell</dc:creator>
		
		<category><![CDATA[Cocoa]]></category>

		<category><![CDATA[category]]></category>

		<category><![CDATA[color]]></category>

		<category><![CDATA[font]]></category>

		<category><![CDATA[NSButton]]></category>

		<category><![CDATA[NSButtonCell]]></category>

		<category><![CDATA[setTextColor]]></category>

		<category><![CDATA[text]]></category>

		<category><![CDATA[textColor]]></category>

		<guid isPermaLink="false">http://www.pastebuffer.com/2007/10/04/setting-the-text-color-of-an-nsbutton/</guid>
		<description><![CDATA[In this era of white-on-black (HUD) interfaces, we often want to change the text-color of NSButtons (including checkboxes and such). NSTextFieldCell has a -setTextColor: method, but NSButtonCell doesn&#8217;t. You can use -setAttributedTitle: to achieve the desired result, but that&#8217;s a bit of a hassle.
Here&#8217;s a simple little category on NSButton which adds -textColor and -setTextColor: [...]]]></description>
			<content:encoded><![CDATA[<p>In this era of white-on-black (HUD) interfaces, we often want to change the text-color of NSButtons (including checkboxes and such). NSTextFieldCell has a <code>-setTextColor:</code> method, but NSButtonCell doesn&#8217;t. You can use <code>-setAttributedTitle:</code> to achieve the desired result, but that&#8217;s a bit of a hassle.</p>
<p>Here&#8217;s a simple little category on NSButton which adds <code>-textColor</code> and <code>-setTextColor:</code> methods to the class.</p>
<p>Header:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co2">#import &lt;Cocoa/Cocoa.h&gt;</span></p>
<p><span class="kw4">@interface</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSButton_Class/index.html"><span class="kw6">NSButton</span></a> <span class="br0">&#40;</span>TextColor<span class="br0">&#41;</span></p>
<p>- <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> *<span class="br0">&#41;</span>textColor;<br />
- <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>setTextColor:<span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> *<span class="br0">&#41;</span>textColor;</p>
<p><span class="kw4">@end</span></div>
<p>Implementation:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co2">#import &quot;NSButton+TextColor.h&quot;</span></p>
<p><span class="kw4">@implementation</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSButton_Class/index.html"><span class="kw6">NSButton</span></a> <span class="br0">&#40;</span>TextColor<span class="br0">&#41;</span></p>
<p>
- <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> *<span class="br0">&#41;</span>textColor<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/index.html"><span class="kw5">NSAttributedString</span></a> *attrTitle = <span class="br0">&#91;</span>self attributedTitle<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> len = <span class="br0">&#91;</span>attrTitle length<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw4">NSRange</span> range = NSMakeRange<span class="br0">&#40;</span><span class="nu0">0</span>, MIN<span class="br0">&#40;</span>len, <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// take color from first char</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/index.html"><span class="kw5">NSDictionary</span></a> *attrs = <span class="br0">&#91;</span>attrTitle fontAttributesInRange:range<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> *textColor = <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> controlTextColor<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>attrs<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; textColor = <span class="br0">&#91;</span>attrs objectForKey:NSForegroundColorAttributeName<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> textColor;<br />
<span class="br0">&#125;</span></p>
<p>
- <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>setTextColor:<span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/index.html"><span class="kw6">NSColor</span></a> *<span class="br0">&#41;</span>textColor<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/index.html"><span class="kw5">NSMutableAttributedString</span></a> *attrTitle = <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/index.html"><span class="kw5">NSMutableAttributedString</span></a> alloc<span class="br0">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;initWithAttributedString:<span class="br0">&#91;</span>self attributedTitle<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw4">int</span> len = <span class="br0">&#91;</span>attrTitle length<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw4">NSRange</span> range = NSMakeRange<span class="br0">&#40;</span><span class="nu0">0</span>, len<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>attrTitle addAttribute:NSForegroundColorAttributeName <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value:textColor <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; range:range<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>attrTitle fixAttributesInRange:range<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self setAttributedTitle:attrTitle<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>attrTitle release<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p>
<span class="kw4">@end</span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.pastebuffer.com/2007/10/04/setting-the-text-color-of-an-nsbutton/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
