<?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>Sorted Bits &#187; Cocoa</title>
	<atom:link href="http://www.sortedbits.com/tag/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sortedbits.com</link>
	<description>objectiveC, C# and more interesting stuff</description>
	<lastBuildDate>Wed, 04 Jan 2012 10:11:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Protected: Localize your UIViews (the easy way)</title>
		<link>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/</link>
		<comments>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 13:21:35 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[cocoa-touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1357</guid>
		<description><![CDATA[There is no excerpt because this is a protected post.]]></description>
			<content:encoded><![CDATA[<form action="http://www.sortedbits.com/wp-pass.php" method="post">
<p>This post is password protected. To view it please enter your password below:</p>
<p><label for="pwbox-1357">Password:<br />
<input name="post_password" id="pwbox-1357" type="password" size="20" /></label><br />
<input type="submit" name="Submit" value="Submit" /></p></form>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Placing curved text on an UIImage</title>
		<link>http://www.sortedbits.com/placing-curved-text-on-an-uiimage/</link>
		<comments>http://www.sortedbits.com/placing-curved-text-on-an-uiimage/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 10:26:51 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[cocoa-touch]]></category>
		<category><![CDATA[curve]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objectivec]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[text]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=1210</guid>
		<description><![CDATA[For a project I am doing for a client, I needed to layout text around a circle in objective-C. I spend a couple of hours searching the internet for this, but somehow when people succeed in doing this, they don&#8217;t post their solution online. So, I made [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I am doing for a client, I needed to layout text around a circle in objective-C. I spend a couple of hours searching the internet for this, but somehow when people succeed in doing this, they don&#8217;t post their solution online.</p>
<p>So, I made an array of NSString objects that I wanted to lay out, around that circle.</p>
<div id="gist-1302242" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">NSArray</span><span class="o">*</span> <span class="n">sections</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSArray</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithObjects:</span><span class="s">@&quot;text1&quot;</span><span class="p">,</span> <span class="s">@&quot;text2&quot;</span><span class="p">,</span> <span class="s">@&quot;text3&quot;</span><span class="p">,</span> <span class="s">@&quot;text4&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">];</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="k">-</span> <span class="p">(</span><span class="n">UIImage</span><span class="o">*</span><span class="p">)</span> <span class="nf">createMenuRingWithFrame:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="nv">frame</span></div><div class='line' id='LC4'><span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// First fix the frame to make sure it uses the right scaling (iPhone 4 / iPad compatibility).</span></div><div class='line' id='LC6'>	<span class="n">frame</span> <span class="o">=</span> <span class="n">CGRectMake</span><span class="p">(</span><span class="n">frame</span><span class="p">.</span><span class="n">origin</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">origin</span><span class="p">.</span><span class="n">y</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">*</span> <span class="n">scale</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">*</span> <span class="n">scale</span><span class="p">);</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Same for the text radius</span></div><div class='line' id='LC9'>	<span class="kt">float</span> <span class="n">scaledTextRadius</span> <span class="o">=</span> <span class="n">textRadius</span> <span class="o">*</span> <span class="n">scale</span><span class="p">;</span></div><div class='line' id='LC10'>	<span class="kt">float</span> <span class="n">scaledRingWidth</span> <span class="o">=</span> <span class="n">ringWidth</span><span class="p">;</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Define the centerpoint of the circle.</span></div><div class='line' id='LC13'>	<span class="n">CGPoint</span> <span class="n">centerPoint</span> <span class="o">=</span> <span class="n">CGPointMake</span><span class="p">(</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">/</span> <span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// self.menuItemsFont is of the type UIFont, which I use to get the font for displaying the text on the circle.</span></div><div class='line' id='LC16'>	<span class="kt">char</span><span class="o">*</span> <span class="n">fontName</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)[</span><span class="n">self</span><span class="p">.</span><span class="n">menuItemsFont</span><span class="p">.</span><span class="n">fontName</span> <span class="nl">cStringUsingEncoding:</span><span class="n">NSASCIIStringEncoding</span><span class="p">];</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// 2 UIColor&#39;s, which I need for the color of the circle and for the text</span></div><div class='line' id='LC19'>	<span class="n">ringColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="nl">colorWithRed:</span><span class="mi">1</span> <span class="nl">green:</span><span class="mi">1</span> <span class="nl">blue:</span><span class="mi">1</span> <span class="nl">alpha:</span><span class="mf">0.7</span><span class="p">];</span></div><div class='line' id='LC20'>	<span class="n">textColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="nl">colorWithRed:</span><span class="mi">0</span> <span class="nl">green:</span><span class="mi">0</span> <span class="nl">blue:</span><span class="mi">0</span> <span class="nl">alpha:</span><span class="mi">1</span><span class="p">];</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>	<span class="n">CGFloat</span><span class="o">*</span> <span class="n">ringColorComponents</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="o">*</span><span class="p">)</span><span class="n">CGColorGetComponents</span><span class="p">(</span><span class="n">ringColor</span><span class="p">.</span><span class="n">CGColor</span><span class="p">);</span></div><div class='line' id='LC23'>	<span class="n">CGFloat</span><span class="o">*</span> <span class="n">textColorComponents</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="o">*</span><span class="p">)</span><span class="n">CGColorGetComponents</span><span class="p">(</span><span class="n">textColor</span><span class="p">.</span><span class="n">CGColor</span><span class="p">);</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'>	<span class="n">CGColorSpaceRef</span> <span class="n">colorSpace</span> <span class="o">=</span> <span class="n">CGColorSpaceCreateDeviceRGB</span><span class="p">();</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextRef</span> <span class="n">context</span> <span class="o">=</span> <span class="n">CGBitmapContextCreate</span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span><span class="p">,</span> <span class="n">colorSpace</span><span class="p">,</span> <span class="n">kCGImageAlphaPremultipliedFirst</span><span class="p">);</span></div><div class='line' id='LC27'><br/></div><div class='line' id='LC28'>	<span class="n">CGContextSetTextMatrix</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">CGAffineTransformIdentity</span><span class="p">);</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>	<span class="n">CGContextSelectFont</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">fontName</span><span class="p">,</span> <span class="mi">18</span> <span class="o">*</span> <span class="n">scale</span><span class="p">,</span> <span class="n">kCGEncodingMacRoman</span><span class="p">);</span></div><div class='line' id='LC31'>	<span class="n">CGContextSetRGBStrokeColor</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">ringColorComponents</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ringColorComponents</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">ringColorComponents</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">ringAlpha</span><span class="p">);</span></div><div class='line' id='LC32'>	<span class="n">CGContextSetLineWidth</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">scaledRingWidth</span><span class="p">);</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'>	<span class="n">CGContextStrokeEllipseInRect</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">CGRectMake</span><span class="p">(</span><span class="n">scaledRingWidth</span><span class="p">,</span> <span class="n">scaledRingWidth</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">-</span> <span class="p">(</span><span class="n">scaledRingWidth</span> <span class="o">*</span> <span class="mi">2</span><span class="p">),</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">-</span> <span class="p">(</span><span class="n">scaledRingWidth</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)));</span></div><div class='line' id='LC35'>	<span class="n">CGContextSetRGBFillColor</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">textColorComponents</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">textColorComponents</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">textColorComponents</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">textAlpha</span><span class="p">);</span></div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'>	<span class="n">CGContextSaveGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC38'>	<span class="n">CGContextTranslateCTM</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">centerPoint</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">centerPoint</span><span class="p">.</span><span class="n">y</span><span class="p">);</span></div><div class='line' id='LC39'><br/></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// define the angles of the texts to divide them evenly on the circle.</span></div><div class='line' id='LC41'>	<span class="kt">float</span> <span class="n">angleStep</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span> <span class="o">/</span> <span class="p">[</span><span class="n">sections</span> <span class="n">count</span><span class="p">];</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// I set the starting-angle on 90 degrees, to make sure the first text was ON TOP of the circle.</span></div><div class='line' id='LC44'>	<span class="kt">float</span> <span class="n">angle</span> <span class="o">=</span> <span class="n">degreesToRadians</span><span class="p">(</span><span class="mi">90</span><span class="p">);</span></div><div class='line' id='LC45'><br/></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Some custom text-radius fixing (and scaling for iPhone 4 &amp;amp; iPad). To make sure my text centered nicely.</span></div><div class='line' id='LC47'>	<span class="n">scaledTextRadius</span> <span class="o">=</span> <span class="n">scaledTextRadius</span> <span class="o">-</span> <span class="mi">12</span> <span class="o">*</span> <span class="n">scale</span><span class="p">;</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Loop through texts and draw them at the appropriate angle.</span></div><div class='line' id='LC50'>	<span class="k">for</span> <span class="p">(</span><span class="n">NSString</span><span class="o">*</span> <span class="n">text</span> <span class="k">in</span> <span class="n">sections</span><span class="p">)</span></div><div class='line' id='LC51'>	<span class="p">{</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// call a method that actually draws the text at an angle</span></div><div class='line' id='LC53'>		<span class="p">[</span><span class="n">self</span> <span class="nl">drawStringAtContext:</span><span class="n">context</span> <span class="nl">string:</span><span class="n">text</span> <span class="nl">atAngle:</span><span class="n">angle</span> <span class="nl">withRadius:</span><span class="n">scaledTextRadius</span><span class="p">];</span></div><div class='line' id='LC54'>		<span class="n">angle</span> <span class="o">-=</span> <span class="n">angleStep</span><span class="p">;</span></div><div class='line' id='LC55'>	<span class="p">}</span></div><div class='line' id='LC56'><br/></div><div class='line' id='LC57'>	<span class="n">CGContextRestoreGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC58'><br/></div><div class='line' id='LC59'>	<span class="n">CGImageRef</span> <span class="n">contextImage</span> <span class="o">=</span> <span class="n">CGBitmapContextCreateImage</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC60'><br/></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Release the stuff to avoid memory leaks.</span></div><div class='line' id='LC62'>	<span class="n">CGContextRelease</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC63'>	<span class="n">CGColorSpaceRelease</span><span class="p">(</span><span class="n">colorSpace</span><span class="p">);</span></div><div class='line' id='LC64'><br/></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="p">[</span><span class="n">UIImage</span> <span class="nl">imageWithCGImage:</span><span class="n">contextImage</span><span class="p">];</span></div><div class='line' id='LC66'><br/></div><div class='line' id='LC67'><span class="p">}</span></div><div class='line' id='LC68'><br/></div><div class='line' id='LC69'><span class="c1">// This method draws the text at an angle, while the location is based on the radius + angle.</span></div><div class='line' id='LC70'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">drawStringAtContext:</span><span class="p">(</span><span class="n">CGContextRef</span><span class="p">)</span> <span class="n">context</span> <span class="nl">string:</span><span class="p">(</span><span class="n">NSString</span><span class="o">*</span><span class="p">)</span> <span class="n">text</span> <span class="nl">atAngle:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">angle</span> <span class="nl">withRadius:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">radius</span></div><div class='line' id='LC71'><span class="p">{</span></div><div class='line' id='LC72'>	<span class="c1">// Scale the fontsize to match UIScreen scaling</span></div><div class='line' id='LC73'>	<span class="n">UIFont</span><span class="o">*</span> <span class="n">scaledMenuItemsFont</span> <span class="o">=</span> <span class="p">[</span><span class="n">menuItemsFont</span> <span class="nl">fontWithSize:</span><span class="mi">18</span> <span class="o">*</span> <span class="n">scale</span><span class="p">];</span></div><div class='line' id='LC74'><br/></div><div class='line' id='LC75'>	<span class="c1">// Get the size of the string when it would be drawn with the selected font</span></div><div class='line' id='LC76'>	<span class="n">CGSize</span> <span class="n">textSize</span> <span class="o">=</span> <span class="p">[</span><span class="n">text</span> <span class="nl">sizeWithFont:</span><span class="n">scaledMenuItemsFont</span><span class="p">];</span></div><div class='line' id='LC77'><br/></div><div class='line' id='LC78'>	<span class="kt">float</span> <span class="n">perimeter</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span> <span class="o">*</span> <span class="n">radius</span><span class="p">;</span></div><div class='line' id='LC79'><br/></div><div class='line' id='LC80'>	<span class="c1">// determine the angle of the text.</span></div><div class='line' id='LC81'>	<span class="kt">float</span> <span class="n">textAngle</span> <span class="o">=</span> <span class="p">(</span><span class="n">textSize</span><span class="p">.</span><span class="n">width</span><span class="p">)</span> <span class="o">/</span> <span class="n">perimeter</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span><span class="p">;</span></div><div class='line' id='LC82'>	<span class="n">angle</span> <span class="o">+=</span> <span class="n">textAngle</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span></div><div class='line' id='LC83'><br/></div><div class='line' id='LC84'>	<span class="c1">// We loop through each letter in the string, so we can set the angle for each letter right</span></div><div class='line' id='LC85'>	<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">index</span> <span class="o">&amp;</span><span class="n">lt</span><span class="p">;</span> <span class="p">[</span><span class="n">text</span> <span class="n">length</span><span class="p">];</span> <span class="n">index</span><span class="o">++</span><span class="p">)</span></div><div class='line' id='LC86'>	<span class="p">{</span></div><div class='line' id='LC87'>	    <span class="c1">// Get the correct letter by the index</span></div><div class='line' id='LC88'>		<span class="n">NSRange</span> <span class="n">range</span> <span class="o">=</span> <span class="p">{</span><span class="n">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">};</span></div><div class='line' id='LC89'>		<span class="n">NSString</span><span class="o">*</span> <span class="n">letter</span> <span class="o">=</span> <span class="p">[</span><span class="n">text</span> <span class="nl">substringWithRange:</span><span class="n">range</span><span class="p">];</span></div><div class='line' id='LC90'>		<span class="kt">char</span><span class="o">*</span> <span class="n">c</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)[</span><span class="n">letter</span> <span class="nl">cStringUsingEncoding:</span><span class="n">NSASCIIStringEncoding</span><span class="p">];</span></div><div class='line' id='LC91'>		<span class="n">CGSize</span> <span class="n">charSize</span> <span class="o">=</span> <span class="p">[</span><span class="n">letter</span> <span class="nl">sizeWithFont:</span><span class="n">scaledMenuItemsFont</span><span class="p">];</span></div><div class='line' id='LC92'><br/></div><div class='line' id='LC93'>	    <span class="c1">// Determin the X and Y position of the letter based on the angle and the radius</span></div><div class='line' id='LC94'>		<span class="kt">float</span> <span class="n">x</span> <span class="o">=</span> <span class="n">radius</span> <span class="o">*</span> <span class="n">cos</span><span class="p">(</span><span class="n">angle</span><span class="p">);</span></div><div class='line' id='LC95'>		<span class="kt">float</span> <span class="n">y</span> <span class="o">=</span> <span class="n">radius</span> <span class="o">*</span> <span class="n">sin</span><span class="p">(</span><span class="n">angle</span><span class="p">);</span></div><div class='line' id='LC96'><br/></div><div class='line' id='LC97'>		<span class="kt">float</span> <span class="n">letterAngle</span> <span class="o">=</span> <span class="p">(</span><span class="n">charSize</span><span class="p">.</span><span class="n">width</span> <span class="o">/</span> <span class="n">perimeter</span> <span class="o">*</span> <span class="o">-</span><span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span><span class="p">);</span></div><div class='line' id='LC98'><br/></div><div class='line' id='LC99'>	    <span class="c1">// Save the state of the context, because rotations are based on the original origin</span></div><div class='line' id='LC100'>		<span class="n">CGContextSaveGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC101'>		<span class="n">CGContextTranslateCTM</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span></div><div class='line' id='LC102'>	    <span class="c1">// Rotate the canvas so we can just draw our text horizontally</span></div><div class='line' id='LC103'>		<span class="n">CGContextRotateCTM</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="p">(</span><span class="n">angle</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">M_PI</span><span class="p">));</span></div><div class='line' id='LC104'>		<span class="n">CGContextShowTextAtPoint</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">c</span><span class="p">));</span></div><div class='line' id='LC105'>	    <span class="c1">// Restore the context again.</span></div><div class='line' id='LC106'>		<span class="n">CGContextRestoreGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC107'><br/></div><div class='line' id='LC108'>		<span class="n">angle</span> <span class="o">+=</span> <span class="n">letterAngle</span><span class="p">;</span></div><div class='line' id='LC109'>	<span class="p">}</span></div><div class='line' id='LC110'><span class="p">}</span></div><div class='line' id='LC111'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302242/0d62efc44eb7d0b2219731bc7a424fd41d4e3103/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302242#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302242">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>I came to this solution after I asked a question on StackOverflow, which was helpfully demonstrated by Dribbel on how to do it. Credits for that to him.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/placing-curved-text-on-an-uiimage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Putting a UIPickerView on a UIActionSheet</title>
		<link>http://www.sortedbits.com/putting-a-uipickerview-on-a-uiactionsheet/</link>
		<comments>http://www.sortedbits.com/putting-a-uipickerview-on-a-uiactionsheet/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 20:24:50 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[uiactionsheet]]></category>
		<category><![CDATA[uipickerview]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=1002</guid>
		<description><![CDATA[Basically there are no really good (and now working) guides on how to put a UIPickerView on a UIActionSheet. Mainly because some things changed with the latest firmware updates made by Apple. Well after some fiddling around, I got it working. Here is how: Step 1: Adding [...]]]></description>
			<content:encoded><![CDATA[<p>Basically there are no really good (and now working) guides on how to put a UIPickerView on a UIActionSheet. Mainly because some things changed with the latest firmware updates made by Apple. Well after some fiddling around, I got it working. Here is how:</p>
<p><strong>Step 1: Adding the right delegates</strong><br />
Make sure your current class (probably a ViewController) uses the following delegates:</p>
<ul>
<li>UIActionSheetDelegate</li>
<li>UINavigationControllerDelegate</li>
<li>UIPickerViewDelegate</li>
<li>UIPickerViewDataSource</li>
</ul>
<p><span id="more-1002"></span></p>
<p><strong>Step 2: Trigger the UIActionSheet</strong><br />
Go to the method where you want to make the UIPickerView appear. In my case it is when I select a row from a table, but of course it could also be when you push a button or anything you want.</p>
<pre class="syntax c">/* first create a UIActionSheet, where you define a title, delegate and a button to close the sheet again */
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@&quot;Select position&quot; delegate:self cancelButtonTitle:@&quot;Done&quot; destructiveButtonTitle:nil otherButtonTitles:nil];
/* I always give my controls a tag, to make sure I can work with multiple actionsheets in one View (to identify them when an event triggers */
actionSheet.tag = POSITION_ROW;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

/* Initialize a UIPickerView with 100px space above it, for the button of the UIActionSheet. */
UIPickerView* positionPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100, 320, 216)];
positionPicker.dataSource = self;
positionPicker.delegate = self;

/* another unique tag for this UIPicker */
positionPicker.tag = POSITION_ROW;

/* Add the UIPickerView to the UIActionSheet */
[actionSheet addSubview:positionPicker];

/* Select the previous selected value, which for me is stored in 'currentPosition' */
[positionPicker selectRow:currentPosition inComponent:0 animated:NO];

/* clean up */
[positionPicker release];

/* Add the UIActionSheet to the view */
[actionSheet showInView:self.view];

/* Make sure the UIActionSheet is big enough to fit your UIPickerView and it's buttons */
[actionSheet setBounds:CGRectMake(0,0, 320, 411)];

/* clean up */
[actionSheet release];</pre>
<p><strong>Step 3: Implementing the datasource and delegates for the UIPickerView</strong><br />
Now, the triggering of the UIActionSheet is now working, but you would also like to have some items in your UIPickerView. Well, take a look at the following code and read the comments.</p>
<pre class="syntax c">/* Defines the total number of Components (like groups in a UITableView) in a UIPickerView */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

/* What to do when a row from a UIPickerView is selected. This will trigger each time you scroll the UIPickerView, so only lightweight stuff. */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	overlayPosition = row;
	[tableSettings reloadData];
}

/* For me the number of items in the UIPickerView is known on compile time. So here I just return 3 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return 3;
}

/* Because the UIPickerView expects a UIView for every row you insert in the UIPickerView, you need to make one. What I do here is really simple. I create a UILabel* and with each row it requests I just change the text in the UILabel. */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
/* Create a UILabel and set it 40 pixels to the right, to make sure it is put nicely in the UIPickerView */
	UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 280, 25)] autorelease];
	label.textColor = [UIColor blackColor];
	label.backgroundColor = [UIColor clearColor];

	switch (row)
	{
		default:
		case kUnderClock:
			label.text = @&quot;Under clock&quot;;
			break;
		case kAboveSlider:
			label.text = @&quot;Above lock slider&quot;;
			break;
		case kCenter:
			label.text = @&quot;Vertically centered&quot;;
			break;
	}

	return label;
}</pre>
<p><strong>Step 4: Do stuff when buttons on the UIActionSheet are clicked</strong><br />
Now, the final thing you have to do is make sure you listen to the events of the UIActionSheet also. So here is the code:</p>
<pre class="syntax c">- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (actionSheet.tag == POSITION_ROW)
	{
/* Do stuff here, only intended for the right UIActionSheet. This only applies for when you use multiple UIActionSheets on the same ViewController. Closing it is unnecessary, because you already specified a destructive button. */
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/putting-a-uipickerview-on-a-uiactionsheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending an email with an attachment from your iPhone</title>
		<link>http://www.sortedbits.com/sending-an-email-with-an-attachment-from-your-iphone/</link>
		<comments>http://www.sortedbits.com/sending-an-email-with-an-attachment-from-your-iphone/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 10:29:48 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[desktop backgrounds hd]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=840</guid>
		<description><![CDATA[For Desktop Backgrounds HD (which is currently in review) I wanted to implement a feature called &#8220;send image by email&#8221;. First I tried to include an image base64 encoded in an URL, with the mailto: protocol, but this resulted in numerous errors. So after a coupe of [...]]]></description>
			<content:encoded><![CDATA[<p>For Desktop Backgrounds HD (which is currently in review) I wanted to implement a feature called &#8220;send image by email&#8221;. First I tried to include an image base64 encoded in an URL, with the mailto: protocol, but this resulted in numerous errors.</p>
<p>So after a coupe of google searches, I came up with the following solution, follow these steps and you will be sending emails with attachments before you can say &#8220;emails with attachments&#8221; 250 times.<br />
<span id="more-851"></span><br />
<strong>Step 1</strong><br />
First add the MessageUI framework in your application.</p>
<p><strong>Step 2</strong><br />
The class you want to use the functionality in, should get the following 2 imports:</p>
<pre>#import
#import </pre>
<p><strong>Step 3</strong><br />
Make sure your class listens to the MFMailComposeViewControllerDelegate.</p>
<p><strong>Step 4</strong><br />
Next up, is to add code to add an attachment to the mail composer and open it up.</p>
<pre>MFMailComposeViewController* composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;

[composer setSubject:@"The subject of the email"];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"test.png"], 1);
[composer addAttachmentData:imageData mimeType:@"image/png" fileName:@"test.png"];
NSString *emailBody = @"Check out this amazing PNG!";
[composer setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:composer animated:YES];
[composer release];</pre>
<p><strong>Step 5</strong><br />
Add this method, to make sure you get back full control over your view-controller:</p>
<pre>- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
	[self dismissModalViewControllerAnimated:YES];
}</pre>
<p>In this last method you can also perform error checking, which is not included in this example. Hope this helps anyone, and if not&#8230; too bad <img src='http://www.sortedbits.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/sending-an-email-with-an-attachment-from-your-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Note to self: how to convert hex codes in a NSString</title>
		<link>http://www.sortedbits.com/note-to-self-how-to-convert-hex-codes-in-a-nsstring/</link>
		<comments>http://www.sortedbits.com/note-to-self-how-to-convert-hex-codes-in-a-nsstring/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 07:23:53 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=828</guid>
		<description><![CDATA[I have been working with Freebase lately and they sometimes encode their strings, so that &#8220;special&#8221; characters are encoded in HEX. For example, sometimes you receive a string like this: Terminator_2$003A_Judgment_Day. I wanted to convert the weird encoding they use, so I wrote a piece of code [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working with Freebase lately and they sometimes encode their strings, so that &#8220;special&#8221; characters are encoded in HEX.</p>
<p>For example, sometimes you receive a string like this: <strong>Terminator_2$003A_Judgment_Day</strong>. I wanted to convert the weird encoding they use, so I wrote a piece of code for that. It might be that this could be encoded some other way and then I would like to hear from you, but here is my code:<br />
<span id="more-828"></span></p>
<pre>
+ (NSString*) replaceHexInString:(NSString*) value
{
	NSRange range = [value rangeOfString:@"$00"];

	while (range.location != NSNotFound)
	{
		if (range.location + 5 &lt;= [value length])
		{
			NSString* hexValue = [value substringFromIndex:range.location];
			hexValue = [hexValue substringToIndex:5];
			NSString* newHexValue = [hexValue stringByReplacingOccurrencesOfString:@&quot;$&quot; withString:@&quot;0x&quot;];

			unsigned int dec;
			NSScanner *scan = [NSScanner scannerWithString:newHexValue];

			if ([scan scanHexInt:&amp;dec])
			{
				NSString* convertedValue = [NSString stringWithFormat:@&quot;%c&quot;, dec];
				value = [value stringByReplacingOccurrencesOfString:hexValue withString:convertedValue];
			}
		}

		range = [value rangeOfString:@&quot;$00&quot;];
	}
	return value;
}
</pre>
<p>I know this probably does not get the UTF-16 characters, but in my case I (hope to) know they won&#8217;t use them.</p>
<p>Well, that&#8217;s about it. Not sure if anyone can use this, but I do <img src='http://www.sortedbits.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/note-to-self-how-to-convert-hex-codes-in-a-nsstring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Queueing HTTP requests in objC</title>
		<link>http://www.sortedbits.com/queueing-http-requests-in-objc/</link>
		<comments>http://www.sortedbits.com/queueing-http-requests-in-objc/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 18:55:51 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=742</guid>
		<description><![CDATA[For WallPaper I download numerous thumbnails from my webserver at the same time. This is causing slowing speeds (to many threads at once), and too much memory usage (too much image data cached at once). So I looked in queueing the downloads and tried (ofcourse) writing my [...]]]></description>
			<content:encoded><![CDATA[<p>For WallPaper I download numerous thumbnails from my webserver at the same time. This is causing slowing speeds (to many threads at once), and too much memory usage (too much image data cached at once).</p>
<p>So I looked in queueing the downloads and tried (ofcourse) writing my own QueueHandler class. After I written and spend a couple of hours bughunting/testing my QueueuHandler, I came to the <strong>awesome</strong> idea to see what the iPhone SDK supplies for this kind of jobs <img src='http://www.sortedbits.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  (you DO read my sarcasm, dont you?).<br />
<span id="more-742"></span><br />
I am using <a title="ASIHTTPRequest Website" href="http://allseeing-i.com/ASIHTTPRequest/" target="_blank">ASIHTTPRequest</a> for my web-requests in WallPaper. I will just show you how I queue my downloads and when the download finishes, I put the image in a predefined and created UIImageView.</p>
<p>I first declare a couple of variables in my <strong>.h</strong> file.</p>
<pre>NSMutableArray* downloadQueue;
NSOperationQueue* queue;</pre>
<p>I added some UIImageView&#8217;s to a view and I put them and the download URL in a NSMutableDictionary, like this:</p>
<pre>NSMutableDictionary* threadArguments = [[NSMutableDictionary alloc] init];

[threadArguments setObject:myUrl forKey:@&quot;url&quot;];
[threadArguments setObject:myImageView forKey:@&quot;iv&quot;];

[self addToDownloadQueue:threadArguments];</pre>
<p>The <strong>addToDownloadQueue</strong> method does something like this after that (<strong>threadArguments</strong> is the parameter the method is receiving).</p>
<pre>// Add the object to the NSMutableArray I got stored.
[downloadQueue addObject:threadArguments];
if (!queue)
{
	queue = [[NSOperationQueue alloc] init];
	[queue setMaxConcurrentOperationCount:1];
}

NSString* threadUrl = [threadArguments valueForKey:@&quot;url&quot;];
NSURL* downloadUrl = [NSURL URLWithString:threadUrl];
ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:downloadUrl];
[request setDelegate:self];
[request setDidFinishSelector:@selector(downloadDone:)];
[request setDidFailSelector:@selector(downloadFailed:)];</pre>
<p>I put the <strong>MaxConcurrentOperationCount</strong> on <strong>1</strong> to make sure the application sends the requests for thumbnails one by one (going easy on your bandwidth and on my server).</p>
<p>Now we need to implement the FinishSelector and the FailSelector methods to catch the downloads that are finished and put the downloaded image in the pre-created UIImageView.</p>
<pre>- (void)downloadFailed:(ASIHTTPRequest *)request
{
	NSLog(@&quot;Downloading failed?&quot;);
}

- (void)downloadDone:(ASIHTTPRequest *)request
{
	NSString* downloadUrl = [request.url absoluteString];

	NSMutableDictionary* foundObj = nil;

	for (NSMutableDictionary* dict in downloadQueue)
	{
		NSString* queueUrl = [dict valueForKey:@&quot;url&quot;];

		if ([downloadUrl isEqualToString:queueUrl])
		{
			UIImageView* iv = (UIImageView*)[dict valueForKey:@&quot;iv&quot;];
			NSData* imageData = request.rawResponseData;

			if (iv != nil)
				iv.image = [UIImage imageWithData:imageData];

			foundObj = dict;
		}
	}

	if (foundObj != nil)
		[downloadQueue removeObject:foundObj];
}</pre>
<p>*Ok, somehow my code syntax highlighting screws up the indenting.*</p>
<p>Well, I hope you can use something of my weird code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/queueing-http-requests-in-objc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

