<?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>mostly a codeBlog</title>
	<atom:link href="http://www.rickigregersen.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rickigregersen.com</link>
	<description>whatever I am currently programming... and I truly digress</description>
	<lastBuildDate>Mon, 21 Jun 2010 15:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Class Methods and Typedef Enums</title>
		<link>http://www.rickigregersen.com/2010/06/21/class-methods-and-typedef-enums/</link>
		<comments>http://www.rickigregersen.com/2010/06/21/class-methods-and-typedef-enums/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 15:22:08 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=263</guid>
		<description><![CDATA[This is a nifty little &#8220;pattern&#8221; for collecting reusable elements, color, labels, settings etc. that is standard through out your program. I do use Interface Builder, but I prefer reuse approach of code. When a projects design has been agreed upon and the basic things are laid out: font, size, color theme etc. I usually [...]]]></description>
			<content:encoded><![CDATA[<p>This is a nifty little &#8220;pattern&#8221; for collecting reusable elements, color, labels, settings etc. that is standard through out your program. I do use Interface Builder, but I prefer reuse approach of code.</p>
<p>When a projects design has been agreed upon and the basic things are laid out: font, size, color theme etc. I usually build myself an ISInterfaceElements class. This class contains a few Class Methods called getColor:(ColorType) type and getLabel:(LabelType) type. Our Proof-of-Concept Class will contain 3 different colors: BackgroundColor, ForegroundColor and TextColor. It will also contain 3 types of labels, a HeadlineLabel, TextLabel and a DetailLabel. These six components will get you pretty far in describing the text and color of an iPhone App.</p>
<h3>How to do it &#8220;manually&#8221;.</h3>
<p>If I were to build a UILabel, using nothing but code, and make it adhere to the design mentioned above it would probably go a bit like this:</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0.0f, 0.0f, 320.0f, 20.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Helvetica&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">14</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; UIColor <span style="color: #002200;">*</span>textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span>0.2f green<span style="color: #002200;">:</span>0.5f blue<span style="color: #002200;">:</span>0.3f alpha<span style="color: #002200;">:</span>1.0f<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span>textColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setShadowColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setShadowOffset<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span>0.0f, 1.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
</pre>
<p>If you do stuff like this manually and scattered around your program you will at some point encounter the &#8220;Let&#8217;s change the font and color of the app!&#8221; witch will leave you with a couple of hours of &#8220;Sherlock Holmes copy-paste&#8221;, as I like to call it.<br />
Instead we will try to gather common functionality in a single class so that any change made here will propagate outwards to all the parts of the code.<br />
We will use the approach that Apple often uses in their frameworks, e.g. when initializing a UIButton you could do it like this:</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; UIButton <span style="color: #002200;">*</span>button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
</pre>
<p>The way to go about this is to write a Class that has Class Methods for getting a UILabel and UIColor back, and instead of giving them ID&#8217;s like 0034, 1234 etc. we write a typedef enum with nice, easy-to-remember names like UIButtonTypeCustom above. This will make sure code completion and compiler checking will work with you instead of against you.</p>
<h3>The Class:</h3>
<p>As I wrote at the beginning our class will have 3 label and 3 colors, so we start out by writing typedef enums for these elements and we define a <strong>(UILabel*) getLabel:(LabelType)</strong> type and <strong>(UIColor*)getColor:(ColorType) type</strong> method.</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;ISInterfaceElements.h</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;BlogPostCode</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Created by RickiG on 6/21/10.</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Copyright 2010 www.rickigregersen.com. Use as you please:).</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<br />
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span><br />
<br />
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">enum</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; BackgroundColor <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>,&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//This will give the BackgroundColor the ID 0 and ForeGroundColor the ID 1 etc.</span><br />
&nbsp; &nbsp; ForegroundColor,&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//it is not needed, if left out it will default to 0,1,2...</span><br />
&nbsp; &nbsp; TextColor,<br />
&nbsp; &nbsp; <br />
<span style="color: #002200;">&#125;</span> ColorType;<br />
<br />
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">enum</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; HeadlineLabel <span style="color: #002200;">=</span> <span style="color: #2400d9;">3</span>,&nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//This will give the HeadlineLabel the ID 3, the TextLabel 4 etc.</span><br />
&nbsp; &nbsp; TextLabel,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//use this only if the enums can not have the same value, in our case</span><br />
&nbsp; &nbsp; DetailLabel,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//they are not needed, but shown to illustrate that enums just corresponds to integers</span><br />
&nbsp; &nbsp; <br />
<span style="color: #002200;">&#125;</span> LabelType;<br />
<br />
<span style="color: #a61390;">@interface</span> ISInterfaceElements <span style="color: #002200;">:</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span style="color: #400080;">NSObject</span></a> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>UIColor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getColor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ColorType<span style="color: #002200;">&#41;</span> type;<br /></span><span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getLabel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LabelType<span style="color: #002200;">&#41;</span> type;<br /></span><br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
</pre>
<p>Now we implement these two methods. The logic of determining which kind of Color of Label is returned is done with a switch case (as the enums are integers):</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;ISInterfaceElements.m</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;BlogPostCode</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Created by RickiG on 6/21/10.</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Copyright 2010 www.rickigregersen.com. Use as you please:).</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<br />
<span style="color: #6e371a;">#import &quot;ISInterfaceElements.h&quot;</span><br />
<br />
<span style="color: #6e371a;">#define APP_FONT @&quot;HelveticaNeue&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Defined once so that it is simple to change the font of the app</span><br />
<span style="color: #6e371a;">#define APP_FONT_BOLD @&quot;HelveticaNeue-Bold&quot;</span><br />
<br />
<span style="color: #a61390;">@implementation</span> ISInterfaceElements<br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>UIColor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getColor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ColorType<span style="color: #002200;">&#41;</span> type <span style="color: #002200;">&#123;</span><br /></span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">int</span> value; <span style="color: #11740a; font-style: italic;">//We deal with the Color in terms of Hex, this is what a designer will often return.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>type<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> BackgroundColor<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0x2d3c3f;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> ForegroundColor<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0x627376;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> TextColor<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0xDDDDDD;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">default</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0x000000;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">int</span> r, g, b;<br />
&nbsp; &nbsp; b <span style="color: #002200;">=</span> value <span style="color: #002200;">&amp;</span> 0x0000FF;<br />
&nbsp; &nbsp; g <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>value <span style="color: #002200;">&amp;</span> 0x00FF00<span style="color: #002200;">&#41;</span> &gt;&gt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; r <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>value <span style="color: #002200;">&amp;</span> 0xFF0000<span style="color: #002200;">&#41;</span> &gt;&gt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span>r<span style="color: #002200;">/</span>255.0f green<span style="color: #002200;">:</span>g<span style="color: #002200;">/</span>255.0f blue<span style="color: #002200;">:</span>b<span style="color: #002200;">/</span>255.0f alpha<span style="color: #002200;">:</span>1.0f<span style="color: #002200;">&#93;</span>; <br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getLabel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LabelType<span style="color: #002200;">&#41;</span> type <span style="color: #002200;">&#123;</span><br /></span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>type<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> HeadlineLabel<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span>APP_FONT_BOLD size<span style="color: #002200;">:</span><span style="color: #2400d9;">14</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self getColor<span style="color: #002200;">:</span>TextColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowOffset <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> TextLabel<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span>APP_FONT size<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor blackColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowOffset <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> DetailLabel<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span>APP_FONT size<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentRight<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">default</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> label;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
</pre>
<h3>Using the ISInterfaceElements Class:</h3>
<p>Now that the class is set up it is really simple to obtain a UILabel or UIColor in a way that 1) does not clutter up your code with repeating setup-code 2) makes your design consistent, fonts and colors are easily changed in one location 3) follows the approach you know from Apples frameworks.</p>
<p>Make sure to import the ISInterfaceElements.h header then do the highlighted part below:</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span> &nbsp; &nbsp;<br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch</span><br />
&nbsp; &nbsp; <br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> <span style="color: #002200;">*</span>headline <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>ISInterfaceElements getLabel<span style="color: #002200;">:</span>HeadlineLabel<span style="color: #002200;">&#93;</span>;<br /></span>&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>headline setText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello, World&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>headline<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2010/06/21/class-methods-and-typedef-enums/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Customizing the UINavigationController</title>
		<link>http://www.rickigregersen.com/2010/06/16/customizing-the-uinavigationcontroller/</link>
		<comments>http://www.rickigregersen.com/2010/06/16/customizing-the-uinavigationcontroller/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 13:45:52 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[Customize]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[UInavigationBar]]></category>
		<category><![CDATA[UInavigationController]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=229</guid>
		<description><![CDATA[I had to add some new tags to my blog as the content will mostly be Cocoa Touch, Objective C, iPhone stuff from now on. I think the UINavigationController is by far the most widely used Controller-controller. Out of the box the API&#8217;s for customizing the looks of the UINavigatioController are limited to setting a [...]]]></description>
			<content:encoded><![CDATA[<p><strong>I had to add some new tags to my blog as the content will <em>mostly </em>be Cocoa Touch, Objective C, iPhone stuff from now on.</strong></p>
<p>I think the UINavigationController is by far the most widely used Controller-controller.</p>
<p>Out of the box the API&#8217;s for customizing the looks of the UINavigatioController are limited to setting a tint for the background color.</p>
<p>I am doing an iPhone app and I would like to have the freedom to set the title label, the color, background image and the buttons to whatever I like. I, however, do not want to get rejected by the App Store approval process or implement my own controller, so in this post I&#8217;ll deliver some <em>Proof-of-Concept</em> code for how to do this.</p>
<h3><strong>The product:</strong></h3>
<div id="attachment_232" class="wp-caption aligncenter" style="width: 392px"><a href="http://www.rickigregersen.com/wp-content/uploads/custom_navbar.png"><img class="size-full wp-image-232" title="Custom UINavigationBar" src="http://www.rickigregersen.com/wp-content/uploads/custom_navbar.png" alt="Yes you are a pretty girl.." width="382" height="265" /></a><p class="wp-caption-text">The finished product</p></div>
<h3><strong>The caveats:</strong></h3>
<p>There are two different approaches involved: one is to build a category on the UINavigationBar, which is the view of the UINavigationController, to set the background to either a static color or a custom image. The other approach is to build a utility Class with one class method that alters the the look of a UIViewControllers UINavigationController. Notice that I wrote that we alter the look of the UINavigationController for each ViewController. There are caveats involved! This is just a Proof of Concept (feel free to add to the code and feel especially free to show me afterwards).</p>
<ul>
<li>It does not support rotation!</li>
<li>The size of the navigationBar buttons are fixed&#8217;!</li>
<li>The background color/image for the UINavigationBar can not be changed at runtime.</li>
<li>You need to implement a tap handler method to catch the actions from the buttons on the navigationBar.</li>
</ul>
<p><em>Probably other things… but it can be added and maybe it will.</em></p>
<h3><strong>The code:</strong></h3>
<p>First off, let&#8217;s construct a category for the UINavigationBar:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;UINavigationBar+customLook.h</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;UINavigationBarOverrideDrawRectPOC</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Created by Ricki Gregersen on 14/6/10.</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Copyright 2010 www.rickigregersen.com. Use as you please:)</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<br />
<span style="color: #6e371a;">#import</span><br />
<br />
<span style="color: #a61390;">@interface</span> UINavigationBar <span style="color: #002200;">&#40;</span>customLook<span style="color: #002200;">&#41;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect;<br />
<span style="display:block;background-color:#ffff66"><br /></span><span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>And the implementation:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &quot;UINavigationBar+customLook.h&quot;</span><br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #a61390;">@implementation</span> UINavigationBar <span style="color: #002200;">&#40;</span>customLook<span style="color: #002200;">&#41;</span><br /></span><br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect <span style="color: #002200;">&#123;</span><br />
<br />
<span style="color: #11740a; font-style: italic;">/* comment in this part to use a color instead of an image</span><br />
<span style="color: #11740a; font-style: italic;">&nbsp; &nbsp; UIColor *color = [UIColor redColor];</span><br />
<span style="color: #11740a; font-style: italic;">&nbsp; &nbsp; CGContextRef context = UIGraphicsGetCurrentContext();</span><br />
<span style="color: #11740a; font-style: italic;">&nbsp; &nbsp; CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));</span><br />
<span style="color: #11740a; font-style: italic;">&nbsp; &nbsp; CGContextFillRect(context, rect);</span><br />
<span style="color: #11740a; font-style: italic;">&nbsp; &nbsp; self.tintColor = [UIColor darkGrayColor];// color;</span><br />
<span style="color: #11740a; font-style: italic;">*/</span><br />
<br />
<span style="color: #11740a; font-style: italic;">// comment out this part to use a color instead of an image</span><br />
&nbsp; &nbsp; UIImage <span style="color: #002200;">*</span>img&nbsp; &nbsp; <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;nav_bar_back.png&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>img drawInRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, self.frame.size.width, self.frame.size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>The drawRect method is invoked every time a UIViewController is pushed onto the screen. You could therefore make global variable that held a reference to the image or color if you needed it to change at runtime.</p>
<p>Now onto the the UINavigaitonModifier Class. This contains one class method that takes a UIViewController, a title string for the left and right button, and a boolean for the left and right button determining if it is a square or an &#8220;arrow&#8221;. (the usual back button is an arrow). The class also defines a protocol for receiving actions when either button is tapped.</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// &nbsp;ISNavigationModifier.h</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;UINavigationBarOverrideDrawRectPOC</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Created by Ricki Gregersen on 14/6/10.</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Copyright 2010 www.rickigregersen.com. Use as you please:)</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<br />
<span style="color: #6e371a;">#import</span><br />
<br />
<span style="color: #a61390;">@protocol</span> ISNavigationModifierProtocol<br />
<br />
@optional<br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> leftBarButtonHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;<br /></span><span style="display:block;background-color:#ffff66"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> rightBarButtonHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;<br /></span><br />
<span style="color: #a61390;">@end</span><br />
<br />
<span style="color: #a61390;">@interface</span> ISNavigationModifier <span style="color: #002200;">:</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span style="color: #400080;">NSObject</span></a> <span style="color: #002200;">&#123;</span><br />
<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> modifyNavigationBarFor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIViewController<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> target <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> leftButtonString <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isLeftArrow <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> rightButtonString <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isRightArrow;<br /></span><br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>The implementation:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// &nbsp;ISNavigationModifier.m</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;UINavigationBarOverrideDrawRectPOC</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Created by Ricki Gregersen on 14/6/10.</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Copyright 2010 www.rickigregersen.com. Use as you please:)</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<br />
<span style="color: #6e371a;">#import &quot;ISNavigationModifier.h&quot;</span><br />
<br />
<span style="color: #a61390;">@implementation</span> ISNavigationModifier<br />
<br />
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> modifyNavigationBarFor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIViewController<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> target <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> leftButtonString <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isLeftArrow <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> rightButtonString <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isRightArrow <span style="color: #002200;">&#123;</span><br />
<br />
&nbsp; &nbsp; UIButton <span style="color: #002200;">*</span>rightButton <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;<br />
&nbsp; &nbsp; UIButton <span style="color: #002200;">*</span>leftButton <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;<br />
<br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>leftButtonString<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br /></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; leftButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftButton addTarget<span style="color: #002200;">:</span>target action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>leftButtonHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftButton.titleLabel setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Thonburi-Bold&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftButton setTitle<span style="color: #002200;">:</span>leftButtonString forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>isLeftArrow<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftButton setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0.0f, 0.0f, 79.0f, 37.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrow_square_btn.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftButton setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0.0f, 0.0f, 79.0f, 37.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;square_btn.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #002200;">&#125;</span><br />
<br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>rightButtonString<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br /></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rightButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightButton addTarget<span style="color: #002200;">:</span>target action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>rightButtonHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightButton.titleLabel setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Thonburi-Bold&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightButton setTitle<span style="color: #002200;">:</span>rightButtonString forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>isLeftArrow<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightButton setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0.0f, 0.0f, 79.0f, 37.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrow_square_btn.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightButton setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0.0f, 0.0f, 79.0f, 37.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;square_btn.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0.0f, 0.0f, 150.0f, 20.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Thonburi-Bold&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">14</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setText<span style="color: #002200;">:</span>target.title<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>target.navigationItem setTitleView<span style="color: #002200;">:</span>label<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label release<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; UIBarButtonItem <span style="color: #002200;">*</span>leftItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithCustomView<span style="color: #002200;">:</span>leftButton<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; UIBarButtonItem <span style="color: #002200;">*</span>rightItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithCustomView<span style="color: #002200;">:</span>rightButton<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>target.navigationItem setLeftBarButtonItem<span style="color: #002200;">:</span>leftItem<span style="color: #002200;">&#93;</span>;<br /></span><span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>target.navigationItem setRightBarButtonItem<span style="color: #002200;">:</span>rightItem<span style="color: #002200;">&#93;</span>;<br /></span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>leftItem release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>rightItem release<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>The modifier class uses the reference to the UIViewController to change the navigationBar.</p>
<h3>Example:</h3>
<p>To use the above classes; put the UINavigationBar+customLook.h and UINavigationBar+customLook.m files somewhere in your project. Change the images or color property to fit your custom style.<br />
Also add the ISNavigationModifier.h and ISNavigationModifier.m.<br />
In a viewController that is pushed onto a NavigationController; import the ISNavigationModifier.h file, implement the methods: leftButtonHandler and rightButtonHandler.</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> viewDidLoad <span style="color: #002200;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; self.title <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;I am a pretty girl&quot;</span>;<br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>ISNavigationModifier modifyNavigationBarFor<span style="color: #002200;">:</span>self <span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Back&quot;</span> <span style="color: #002200;">:</span><span style="color: #a61390;">NO</span> <span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Next&quot;</span> <span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;<br /></span><span style="color: #002200;">&#125;</span><br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> leftButtonHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span><br /></span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.navigationController popViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Left Button Tapped!&quot;</span><span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> rightButtonHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span><br /></span><br />
&nbsp; &nbsp; ISViewControllerThree <span style="color: #002200;">*</span>vcThree <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ISViewControllerThree alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ISViewControllerThree&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.navigationController pushViewController<span style="color: #002200;">:</span>vcThree animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>vcThree release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Right Button Tapped!&quot;</span><span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>Feel free to put  rants, comments, questions and improvements in the comments below.    </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2010/06/16/customizing-the-uinavigationcontroller/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>No More Virtual Machines</title>
		<link>http://www.rickigregersen.com/2010/01/17/no-more-virtual-machines/</link>
		<comments>http://www.rickigregersen.com/2010/01/17/no-more-virtual-machines/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 07:21:09 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Objective C]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=224</guid>
		<description><![CDATA[Sorry for the 3 month dry streak. I was fired back in, think it was August? . I then had a three month &#8220;paid vaccination&#8221;(it was in my contract that in the event I was fired they had to pay me for 3 months but I didn&#8217;t have to come to work&#8230;sweet). Did some consulting [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for the 3 month dry streak.</p>
<p>I was fired back in, think it was August? <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . I then had a three month &#8220;paid vaccination&#8221;(it was in my contract that in the event I was fired they had to pay me for 3 months but I didn&#8217;t have to come to work&#8230;sweet). Did some consulting work, mostly to have something to do. Then I thought to my self that this was an excellent time to set a dream of mine in motion. I come from a background in computer and electronic engineering.</p>
<p>In my Flash/Java work I always had this strange feeling in the back of my head going &#8220;At some point this is going to break or perform strangely, and I have no way, other that trial and error, to figure out why&#8221;. This is harshly put, but I have been used to dealing with µControllers, the C language and copper-wire  for a long time and even though I appreciate things that &#8220;just work&#8221; as much as the next guy, I also feel the complete despair when it does not and you have no way of getting &#8220;under the hood&#8221;.</p>
<p>Im probably being ignorant and if I would I could sit down and read up on every nook and cranny of the runtime, then I would be the all-omnipotent, all-mighty and know-it-all master of&#8230; a nook.</p>
<p>My previous job involved building Flash apps for the AIR  runtime environment, I know, I know, that is asking for trouble(2007-2009), and my only real choice of an editor was Eclipse. Im a Mac user and that just &#8220;grinds my gears&#8221; to have to use some &#8220;lowest common denominator&#8221; GUI for what I do with most of the hours Im awake.</p>
<p>I have a duality in my that dictates that I love building stuff for the internal intricacies and that I only can enjoy software, furniture, food TV series etc. if I know the order and beauty that governs the inside also governs the outside, i.e. I like good design on both the inside and the outside.</p>
<p>So how do I get to work with µControllers and hardware, beautiful interfaces and have a set of development tools that doesn&#8217;t hate me and gives me a stack dump when I ask for it?</p>
<p><strong>I have become an iPhone developer!</strong></p>
<p>OK, plan&#8230; by rationing my means I can sustain myself for almost a year. In that timeframe I should be able to (a) learn Objective C (b) get a few apps in the store (c) go down new roads and be happy:)</p>
<p>So for the last three months I have been developing an app that will _blank_ for _blank_ when you go _blank_, with a designer called Mino, which is a secretive guy, hence the _blank_ <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now we are close to a Beta version and I my head is exploding with pieces of info that I would like to put on the blog. Im at the state of learning a new language where I bought the first &#8220;pattern book&#8221;, which means I did a lot of <em>&#8220;ahh this seems to be the way to go&#8221; </em>only to go <em>&#8220;ahh so that is how I should have done it&#8221;</em> a few hours later. Well, to cut it short, I don&#8217;t think there will be a lot of AS &#8211; Java stuff on the blog in the time to come, but I hope to keep doing the visual experiments and give out some code snippets.</p>
<p>I&#8217;ll surely move on to building some Mac software afterwards, I like the tools, the Apple frameworks has the standard of the Apple hardware (95% perfect and 5% &#8220;WTF, this took me forever and now it&#8217;s broken again&#8221;) also I get the &#8220;complete package experience&#8221; that I was missing in my other endeavors.</p>
<p>So please spread the word that Im up for crazy and exciting projects, both here in DK and abroad.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2010/01/17/no-more-virtual-machines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Packing Circles &#8211; The Stupid Way</title>
		<link>http://www.rickigregersen.com/2009/09/21/packing-circles-the-stupid-way/</link>
		<comments>http://www.rickigregersen.com/2009/09/21/packing-circles-the-stupid-way/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 22:01:56 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Processing]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[packing]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=221</guid>
		<description><![CDATA[This is a small experiment I did on &#8220;packing circles&#8221;. The circles are not really packed, the program just keeps suggesting circles of varying sizes in different positions on the screen and then tests to see if they will fit. If they fit they are drawn if not they are discarded. Example and code here]]></description>
			<content:encoded><![CDATA[<p>This is a small experiment I did on &#8220;packing circles&#8221;. The circles are not really packed, the program just keeps suggesting circles of varying sizes in different positions on the screen and then tests to see if they will fit. If they fit they are drawn if not they are discarded.</p>
<div id="attachment_222" class="wp-caption aligncenter" style="width: 521px"><a href="http://www.rickigregersen.com/wp-content/uploads/circlePacking.png"><img class="size-full wp-image-222" title="circlePacking" src="http://www.rickigregersen.com/wp-content/uploads/circlePacking.png" alt="A small 5 min. run of the program" width="511" height="510" /></a><p class="wp-caption-text">A small 5 min. run of the program</p></div>
<p><a href="http://www.rickigregersen.com/wp-content/circleapplet/">Example and code here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/09/21/packing-circles-the-stupid-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DTXplorer drum kit goes Processing</title>
		<link>http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/</link>
		<comments>http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 00:01:27 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[drum]]></category>
		<category><![CDATA[DTXplorer]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[MIDI]]></category>
		<category><![CDATA[Midipipe]]></category>
		<category><![CDATA[OSC]]></category>
		<category><![CDATA[Osculator]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=199</guid>
		<description><![CDATA[This my DTXplorer drumkit getting feed into MidiPipe onto Osculator and finally ends up in Processing. Now thats beat detection! I have taken some time to get my Yamaha DTXplorer electronic drums to communicate nicely with my Mac, finally it worked out and I can now &#8220;harvest&#8221; the sweet, sweet MIDI coming from the drums. [...]]]></description>
			<content:encoded><![CDATA[<h5>This my DTXplorer drumkit getting feed into MidiPipe onto Osculator and finally ends up in Processing. Now thats beat detection!</h5>
<p><a href="http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/"><em>Click here to view the embedded video.</em></a></p><br />
<p><a href="http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/"><em>Click here to view the embedded video.</em></a></p>
<p>I have taken some time to get my <a href="http://www.yamaha.com/drums/drumproductdetail.html?CNTID=544640&amp;CTID=5040587">Yamaha DTXplorer</a> electronic drums to communicate nicely with my Mac, finally it worked out and I can now &#8220;harvest&#8221; the sweet, sweet MIDI coming from the drums. Im no Moby, so believe me when I say these last one and a half days of frustrations was not to immortalize the sound of my drumming. It was, of course to get the data into <a href="http://www.processing.org">Processing</a>. A friend of mine is a DJ and he wanted us to experiment with playing &#8220;live&#8221; drums along with the turntables. Furthermore, to make some sort of visualization that would be 100% true to the beat. This is very hard I found out, there are a lot of good sound libraries with beat detection out there, but they are never plug n&#8217; play. You need to tweak your code for every single song and even that is no guarantee for a consistent output. I came up with a pretty straight forward solution, if we were to use the electronic drums anyway we could just as well use the MIDI for the beat detection and then have the turntables control the colors.</p>
<p>I found out that I needed a few different applications and a MIDI cable before I got the signal all the way through. First off is the <a href="http://www.esi-audio.com/products/midimate2/">ESI MidiMate</a> cable, I actually had the old model, but I think it was broken and could not handle if the MIDI in was not connected to a ground plane. It was 295 Danish Kroner ( ≈$55 ). Then I needed <a href="http://web.mac.com/nicowald/SubtleSoft/MidiPipe.html">MidiPipe</a>. This a free application developed by Nico Wald and it is brilliant! Should I ever make any money with this setup I will run to a browser and donate some money to Nico.<br />
The idea with MidiPipe is that it &#8220;hijacks&#8221; every single MIDI port it can detect on the system, you then drag the one you need to the pipe(ESI MIDIMATE Port 1 turn up in MidiPipe the second I connected it to the drumkit) you can then drag other stuff into the pipe. I dragged in &#8220;AList&#8221; from the &#8220;Modifier&#8221; and that gave me just what it says, a list of all midi signals passing through MidiPipe.</p>
<div id="attachment_201" class="wp-caption aligncenter" style="width: 506px"><a href="http://www.rickigregersen.com/wp-content/uploads/midipipe.png"><img class="size-full wp-image-201" title="midipipe" src="http://www.rickigregersen.com/wp-content/uploads/midipipe.png" alt="MidiPipe is a life-saver" width="496" height="512" /></a><p class="wp-caption-text">MidiPipe is a life-saver</p></div>
<p>This was a cool way of testing out the drums. Lastly I dragged a &#8220;MIDI out&#8221; into the Pipe, more about that in a second. Then I went out and finally bought that license for <a href="http://www.osculator.net/wp/">OSCulator</a>. Osculator have been running my Wiimote, iPhone, Arduino and Make controller for the last year and I have been to lazy/cheap to just buy the license. Well Osculator is a $15 minimum, but Camille asks people nicely to pay $39, which I did, of course, having used his software for about a year now. What Osculator can do is just to much to explain here&#8230; I needed it to translate my MIDI into OSC (because OSC is the new MIDI <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) and then relay the rewritten data to a port on localhost using UDP. This was done so I could have some control over the data, also I now get something along the lines of &#8220;snare &#8211; on &#8211; 0.6&#8243; (thats the pad sending the message, that it was &#8220;on&#8221;/hit and the strength it was hit with) into Processing instead of some MIDI byte I have to bitwise shift around to get any data from it, it just makes debugging easier and sending something through Osculator opens a world of other possibilities.</p>
<div id="attachment_204" class="wp-caption aligncenter" style="width: 522px"><a href="http://www.rickigregersen.com/wp-content/uploads/osculator1.png"><img class="size-full wp-image-204" title="osculator" src="http://www.rickigregersen.com/wp-content/uploads/osculator1.png" alt="It is just crazy how many features there can be in such a simple application" width="512" height="484" /></a><p class="wp-caption-text">It is just crazy how many features there can be in such a simple application</p></div>
<p>A note here, when I started Osculator, MidiPipe noticed Osculator was listening on port 8000 and stuffed that piece of information into the &#8220;MIDI out&#8221; I mentioned earlier, now I could just select it there. So now my MIDI came in through MidiPipe, was listed for me to see and then relayed on to Osculator. In Osculator I caught each MIDI event and by selecting the signal and going Osculator &#8211; Edit &#8211; Demux, I could split the signal into its components and rebroadcast them as OSC messages on the network. For handling OSC Protocol stuff in Processing Andreas Schlegel did a really great library, you can get it <a href="http://www.sojamo.de/libraries/oscP5/">here</a> along with some other goodies Andreas made.</p>
<p>I now had 8 different OSC messages, one for each drum pad, cymbal and foot pedal (Im going to do the high hat pedal later on) that I can read out in Processing. I did a small sketch as you see in the video and just started the &#8220;play along&#8221; feature on my DTXplorer kit, with some &#8220;Hard Rock&#8221; <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  selected and and the computers microphone recording.</p>
<p>And as always if there is some code or help You need just leave a mail or comment.</p>
<p><a href="http://www.rickigregersen.com/wp-content/uploads/midiProcessing.mov">A version not completely compressed by the geniuses over at youtube</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
<enclosure url="http://www.rickigregersen.com/wp-content/uploads/midiProcessing.mov" length="1515678" type="video/quicktime" />
		</item>
		<item>
		<title>Fake Gravity Fluid</title>
		<link>http://www.rickigregersen.com/2009/08/31/fake-gravity-fluid/</link>
		<comments>http://www.rickigregersen.com/2009/08/31/fake-gravity-fluid/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 22:01:41 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Particle]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[flow field]]></category>
		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=196</guid>
		<description><![CDATA[I was playing around with the Traer Physics Library for processing/java. It is really fast but even though I had done some small experiments with it a year or so back, I still had to dig out the manual and refresh some basic stuff. Well Im looking into the whole FluidSolver thing, again, and I [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing around with the <a href="http://www.cs.princeton.edu/~traer/physics/">Traer Physics Library</a> for processing/java. It is really fast but even though I had done some small experiments with it a year or so back, I still had to dig out the manual and refresh some basic stuff.<br />
Well Im looking into the whole FluidSolver thing, again, and I thought it could be fun to do a fast and crude Newtonian approach and just go for an effect that looks a bit like it, but is far more simple.</p>
<p><img src="http://www.rickigregersen.com/wp-content/uploads/fluid.png" alt="16834 particles fakes fluid" /></p>
<p><a href="http://www.rickigregersen.com/wp-content/uploads/fluid_applet/index.html">Example and code here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/31/fake-gravity-fluid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SporeTree</title>
		<link>http://www.rickigregersen.com/2009/08/11/sporetree/</link>
		<comments>http://www.rickigregersen.com/2009/08/11/sporetree/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 21:21:02 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[spore]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=193</guid>
		<description><![CDATA[This time the spores turned into a tree. It is by no means perfect, the branches does not move upwards with the stem as it should, were this a real orange colored scary spore tree. This was just some &#8220;while Im doing nothing, let&#8217;s put whatever in the &#8216;fun&#8217; category&#8221; but if anyone would like [...]]]></description>
			<content:encoded><![CDATA[<p>This time the <a href="http://www.rickigregersen.com/?paged=2">spores</a> turned into a tree. It is by no means perfect, the branches does not move upwards with the stem as it should, were this a real orange colored scary spore tree.</p>
<p>This was just some &#8220;while Im doing nothing, let&#8217;s put whatever in the &#8216;fun&#8217; category&#8221; <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  but if anyone would like the code, throw me a comment and I&#8217;ll send it off. I is uncommented and messy.</p>
<p><object width="512" height="750" data="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowingTree.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowingTree.swf" /></object></h4>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/11/sporetree/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Circle Intersection</title>
		<link>http://www.rickigregersen.com/2009/08/09/circle-intersection/</link>
		<comments>http://www.rickigregersen.com/2009/08/09/circle-intersection/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 21:17:42 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Processing]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[Perlin]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=190</guid>
		<description><![CDATA[&#8220;What is the point&#8221; was my first thought on this. I had to do some calculations on the intersections between circles, that was pretty straight forward and I got a class written up in Processing that could do just that. Then I started playing around and ended up moving 10 circles around the screen using [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8220;What is the point&#8221;</p></blockquote>
<p> was my first thought on this.<br />
I had to do some calculations on the intersections between circles, that was pretty straight forward and I got a class written up in Processing that could do just that.<br />
Then I started playing around and ended up moving 10 circles around the screen using Perlin noise and if any of them intersects, I draw a dot. Pointless? certainly!</p>
<p>Maybe there is someone out there with an idea on how to make this into something other that a cpu hogging math feast. I would love to hear from You:)</p>
<p>Well there is some Perlin movement, some ArrayList looping and of course that class that takes 2 circles and returns their intersection, maybe someone can use that for something useful.<br />
<img src="http://rickigregersen.com/wp-content/uploads/circleIntersection.png" alt="A short run, maybe it get's better?" /></p>
<p><a href="http://www.rickigregersen.com/wp-content/uploads/circleApplet/index.html">Example and code here:</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/09/circle-intersection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing Tentacles</title>
		<link>http://www.rickigregersen.com/2009/08/08/processing-tentacles/</link>
		<comments>http://www.rickigregersen.com/2009/08/08/processing-tentacles/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 16:16:38 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Particle]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=185</guid>
		<description><![CDATA[I just did a quick port to processing, important to notice that these classes are &#8220;as is&#8221;, but still, the speed of Java compared to Flash is&#8230; well lets just say the gap isn&#8217;t quite gone yet EDIT: forgot some int/float math that made the tentacles look bulky&#8230; better now. Example and code here:]]></description>
			<content:encoded><![CDATA[<p>I just did a quick port to processing, important to notice that these classes are &#8220;as is&#8221;, but still, the speed of Java compared to Flash is&#8230; well lets just say the gap isn&#8217;t quite gone yet <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>EDIT: forgot some int/float math that made the tentacles look bulky&#8230; better now.<br />
<a href="http://www.rickigregersen.com/wp-content/uploads/applet/index.html">Example and code here:</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/08/processing-tentacles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Growing Homegrown Tentacles</title>
		<link>http://www.rickigregersen.com/2009/08/08/growing-homegrown-tentacles/</link>
		<comments>http://www.rickigregersen.com/2009/08/08/growing-homegrown-tentacles/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 23:30:19 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=183</guid>
		<description><![CDATA[Well, I just got time to look into how to get the tentacles growing, i.e. start out small and have all the joints gradually increase in size, instead of the joint getting smaller all the time. (think I just tried to explain the definition of growing by using the definition of growing??). Thanks again to [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I just got time to look into how to get the tentacles growing, i.e. start out small and have all the joints gradually increase in size, instead of the joint getting smaller all the time. (think I just tried to explain the definition of growing by using the definition of growing??).</p>
<p>Thanks again to <a href="http://blog.soulwire.co.uk/">Justin</a>.</p>
<p><object width="512" height="512" data="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowing.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowing.swf" /></object></h4>
<p>The spore Class turned out to look like this, it needs going over, but I was thinking on porting it to Processing and really get my tentacles on <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The code:</p>
<pre lang="actionscript">
package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.setTimeout;

	public class Spore extends Sprite
	{
		private static const LENGTH:int = 90;
		private var _posX:Vector.<Number>;
		private var _posY:Vector.<Number>;
		private var angStep:Number;
		private var posStep:Number;
		private var curl:Number;
		private var n:Number = 0;
		private var a:Number = Math.random() * Math.PI * 2;
		private var _index:int;
		private var _g:Graphics;

		public function Spore()
		{
			_posX = new Vector.<Number>( LENGTH, true );
			_posY = new Vector.<Number>( LENGTH, true );
			_g = graphics;
			this.generate( range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ) );
		}

		public function generate( aStep:Number, pStep:Number, c:Number ):void
		{
			_index = 0;
			angStep = aStep;
			posStep = pStep;
			curl = c;
			var loc:Point = new Point( 0, 0 );
			while( _index < LENGTH )
			{
				n += Math.random() * (angStep - -angStep) + -angStep;
				n *= 0.9 + curl * 0.1;
				a += n;
				loc.x += Math.cos( a ) * posStep;
				loc.y += Math.sin( a ) * posStep;
				_posX[ _index ] = loc.x;
				_posY[ _index ] = loc.y;
				_index++;
			}
			_index = 0;
		}
		public function step():void
		{
			_g.clear();
			_g.lineStyle( 0, 0x000000, 0.7 );
			var c:int = 0;
			var radius:Number;
			while( c < _index )
			{
				radius = _index / c;
				if( radius > 10 ) radius = 10;
				_g.beginFill( 0xFF6600, 0.4 );
				_g.drawCircle( _posX[ c ], _posY[ c ], radius );
				_g.endFill();
				c++;
			}
			_index++;
			if( _index >= LENGTH )
			{
				this.generate( range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ) );
			}
		}
		private function range(min:Number, max:Number = NaN):Number
		{
			if(isNaN(max)) {max = min; min = 0}
				return min + (Math.random() * (max - min));
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/08/growing-homegrown-tentacles/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
