<?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>James Van Dyne &#187; Objective-C</title>
	<atom:link href="http://www.james-vandyne.com/category/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.james-vandyne.com</link>
	<description>The Life and Times</description>
	<lastBuildDate>Sat, 08 Oct 2011 16:11:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Thoughts on Xcode 4 Price</title>
		<link>http://www.james-vandyne.com/thoughts-on-xcode-4-price/</link>
		<comments>http://www.james-vandyne.com/thoughts-on-xcode-4-price/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 06:18:42 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.james-vandyne.com/?p=184</guid>
		<description><![CDATA[With the release of Xcode 4 came a completely unknown feature, even for developers like myself: a price tag. This is the first time that Xcode has ever had a price. Since the release of OS X, Xcode (and its &#8230; <a href="http://www.james-vandyne.com/thoughts-on-xcode-4-price/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With the release of Xcode 4 came a completely unknown feature, even for developers like myself: a price tag. This is the first time that Xcode has ever had a price. Since the release of OS X, Xcode (and its predecessor Project Builder) have been available free of charge.</p>

<p>At the time, having a full, professional grade IDE available for free was mostly unheard of. Compared with Visual Studio, who&#8217;s minimum entrance fee was in the range of $1000. Students, of course, got a discount. Since then having a free IDE on your platform has become the norm. If this is because of Xcode or not, I am not sure.</p>

<p>Suddenly, for the first time, Xcode has a price for non-paid developers: $4.99. There has been an uproar as to what open source developers and people who are interested in learning to program will do. That Apple is somehow taking away a lot from developers by adding this barrier to entry. I would argue, they aren&#8217;t.</p>

<p>Open source developers often list Xcode as a dependency for those that wanted to compile from source, not because the project required Xcode, but because it installed the requisite compilers. If having Xcode as a dependency is  not something that developers want, then the community is going to need to pick up the slack as per keeping the toolchain current and packaged.  As these tools are open source, it shouldn&#8217;t be that difficult to do (although we do know what happens when we say shouldn&#8217;t&#8230;.)</p>

<p>Ignoring the argument that if you can afford a Mac, and you&#8217;re interested in learning to program, you can afford $5 dollars for tools to develop with. More than likely, the book you pick up to learn Objective-C will cost a heck of a lot more than that.</p>

<p>At the end of the day, any serious mac developer is going to have the $99 dollar subscription to the Mac developer program. If you&#8217;re looking to learn to program for the Mac, 5 dollars is a minimal investment for access to a world class development tools. If you&#8217;re just looking to program on your Mac but don&#8217;t want to spend the $5 for Xcode or $99 for the developer subscription, learn a language different than Objective-C. There are many build in programming languages in OSX, including Python and Ruby.</p>

<p>As for me? I will keep my paid developer accounts ignore the rest of the uproar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.james-vandyne.com/thoughts-on-xcode-4-price/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Make an Accessible Multiline NSMenuItem (without any custom drawing)</title>
		<link>http://www.james-vandyne.com/how-to-make-an-accessible-multiline-nsmenuitem-without-a-custom-view/</link>
		<comments>http://www.james-vandyne.com/how-to-make-an-accessible-multiline-nsmenuitem-without-a-custom-view/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 05:57:15 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.james-vandyne.com/?p=159</guid>
		<description><![CDATA[In developing a new application, I ran into the need for a NSPopupButton for selecting a preference. The menu that you see popup when you click on a NSPopupButton is really just an NSMenu full of NSMenuItems. The same menu &#8230; <a href="http://www.james-vandyne.com/how-to-make-an-accessible-multiline-nsmenuitem-without-a-custom-view/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In developing a new application, I ran into the need for a NSPopupButton for selecting a preference. The menu that you see popup when you click on a NSPopupButton is really just an NSMenu full of NSMenuItems. The same menu items that you might see in a contextual menu or even from the main menu at the top of the screen.</p>

<p>I was looking for some way to display a Title as regular, then right under it some kind of subtitle. Naturally, NSMenuItems are designed to handle just a single title. It seemed like if I wanted to display anything other than a single title, I was going to have to take advantage of NSMenuItem&#8217;s new ability to host a generic NSView.</p>

<h3>Problem</h3>

<p>Getting your custom NSView in to a NSMenuItem is easy. I actually got it displaying rather nicely. The problems came when I wanted my NSView and NSMenuItem to behave like they would as per regular.</p>

<ol>
<li><p>Passing clicks from my NSView to the NSPopupButton proved to be rather difficult. Moreover, it felt like a huge kludge as it tied these items to a single control. I also wanted to reuse these in the menu up top.</p></li>
<li><p>Even with the kludge, I decided to proceed on. Next up was displaying the checkbox (NSOnState) image. For some reason, the checkbox image returned by [NSImage imageNamed:NSOnStateImage] is different from the checkbox image used in the rest of the system. It was skinner and looked out of place.</p></li>
</ol>

<p>Overall, making a custom view with this much kludge and not quite feeling right grated on me. I wasted 2 days to get this far, but it doesn&#8217;t live up to the user experience that I want to give users and expect from software on the Mac.</p>

<h3>Solution</h3>

<p>In my rush to get my NSMenuItem running, I overlooked a a critical feature: NSMenuItem titles can take an NSAttributedString.</p>

<p>When initializing your NSMenuItem we can use an attributed string to set our colors. \n also works for creating new lines.</p>

<pre class="objc">
// assume we have an NSMenuItem alloc'ed/init'ed named item.
// we also have a model objected that responds to the "title" and "subtitle" messages 
// named myObject

NSString *title = [NSString stringWithFormat:@"%@\n%@",[myObject title],[myObject subtitle]];
NSMutableAttributedString *attributed_title = [[NSMutableAttributedString alloc] initWithString:title];
        
// This is the system default for controls. anything else and it looks off
NSDictionary *title_options = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Lucida Grande" size:13.0f],NSFontAttributeName,nil];

//make our subtitle a different color as it is just auxillary information 
NSDictionary *sub_title_options = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor disabledControlTextColor],NSForegroundColorAttributeName,nil];

// apply our color attributes to the ranges of the string they are applicable to...
[attributed_title addAttributes:title_options range:[title rangeOfString:[myObject title]]];
[attributed_title addAttributes:sub_title_options range:[title rangeOfString:[myObject subtitle]]];

// finally set our attributed to the menu item
[item setAttributedTitle:attributed_title];
[item setRepresentedObject:myObject];

// clean up
[attributed_title release];
</pre>

<p>With the above in place we are halfway there. Our new shiny menu item looks great and it fits in. Most importantly there is zero kludge. Actions go where we want them when we want them. The NSOnState image looks like it does in the rest of the system. Life is hunky-dory.</p>

<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.james-vandyne.com/wp-content/uploads/2011/02/nsmenuitem_no_highlight.png" alt="Our multiline NSMenuItem" title="Our multiline NSMenuItem" border="0" width="193" height="44" /></p>

<p>Until we highlight it.</p>

<p>Our title and state images turn white when highlighted as expected. However, our subtitle&#8230;not so much.</p>

<p>Luckily we can fix this without too much trouble. NSMenu&#8217;s has a lovely delegate that tells us when it is about to highlight something with it&#8217;s menu:willHighlightItem: call. &#8220;Will&#8221; is the keyword that we tips us of we can use this. It means that our currently highlighted item is still selected, so we can revert any changes made.</p>

<p>*Update: * I had left a huge block of code that smelled bad and left it as an exercise to the reader to clean it up. I decided to remove some of the smell myself. If you have ideas on how the code below could be refactored even better, please let a comment below.</p>

<pre class="objc">
// set our controller to be the delegate of our NSMenu. In the case of a NSPopupButton, 
// you can access it with the [myButton menu] method.

- (void)setForegroundColor:(NSColor *)foregroundColor 
    onAttributedString:(NSMutableAttributedString *)attributedString 
    forRangeOfString:(NSString *)string {

    NSRange range = [[attributedString mutableString] rangeOfString:string];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:foregroundColor,NSForegroundColorAttributeName,nil];
    
    [attributedString setAttributes:attributes range:range];
    
}

- (void)willHighlightItem:(NSMenuItem *)item currentlyHighlightedItem:(NSMenuItem *)currentItem {
    
    if (currentItem != nil) {
        NSString *subtitle = [(MyModelObject *)[currentItem representedObject] subtitle];
        NSMutableAttributedString *attributedString = [[currentItem attributedTitle] mutableCopy];

        [self setForegroundColor:[NSColor disabledControlTextColor] onAttributedString:attributedString forRangeOfString:subtitle];
                
        [currentItem setAttributedTitle:attributedString];
        [attributedString release];
    }
    
    NSString *subtitle = [(MyModelObject *)[item representedObject] subtitle];
    NSMutableAttributedString *attributedString = [[item attributedTitle] mutableCopy];
    
    [self setForegroundColor:[NSColor selectedMenuItemTextColor] onAttributedString:attributedString forRangeOfString:subtitle];

    [item setAttributedTitle:attributedString];
    [attributedString release];
}

</pre>

<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://james-vandyne.com/wp-content/uploads/2011/02/nsmenuitem_highlight.png" alt="Our nicely highlighted NSMenuItem"/></p>

<h3>Conclusion</h3>

<p>With just the few lines of code above, we can create a custom looking NSMenuItem that behaves exactly as we would expect it to. What&#8217;s more by using an NSAttributedString isntead of a custom view, our &#8220;custom control&#8221;  get accessibility for free. This means users with impaired sight get the same first-class experience as everyone else. Talk about a win-win-win.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.james-vandyne.com/how-to-make-an-accessible-multiline-nsmenuitem-without-a-custom-view/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Choose the Right Tool (Everything is not a nail)</title>
		<link>http://www.james-vandyne.com/choose-the-right-tool-everything-is-not-a-nail/</link>
		<comments>http://www.james-vandyne.com/choose-the-right-tool-everything-is-not-a-nail/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 02:37:30 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.james-vandyne.com/?p=152</guid>
		<description><![CDATA[Picking the right tool for the job is often rather difficult for software developers. As software developers,We tend to find a hammer we like and treat everything like a nail, even if it&#8217;s a screw. Sometimes we&#8217;re reluctant to learn &#8230; <a href="http://www.james-vandyne.com/choose-the-right-tool-everything-is-not-a-nail/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Picking the right tool for the job is often rather difficult for software developers. As software developers,We tend to find a hammer we like and treat everything like a nail, even if it&#8217;s a screw. Sometimes we&#8217;re reluctant to learn new and different tools or ways of hammering.</p>

<p>When I first started writing <a href="http://www.sugoisoft.com/jisho">Jisho</a>, my hammer of choice was Objective-C. All projects, no matter their suitability, were developed in Objective-C. I even wished that Apple would have continued NeXT&#8217;s support for <a href="http://en.wikipedia.org/wiki/WebObjects">WebObjects</a> in Objective-C. Afterall, everything is nail.</p>

<p>This meant that things that probably should have never been written in Objective-C were written in Objective-C. That&#8217;s not to say that the things I wrote couldn&#8217;t be written in Objective-C, indeed, they could, but that it would have been better to pick a different tool. A rock can break a piece of wood in two, which is great if you just need two random lengths of wood, but if you need any more precision, you should use a saw. Objective-C and Python can both parse xml, but the former takes a lot more effort than the later.</p>

<p>I eventually came to my senses and realized that my supporting application was not scaling the way I had hoped. It needed to be rewritten. What did I do? I picked the same hammer again, foolishly expecting different results.</p>

<p>It worked great for a while &#8211; until it didn&#8217;t. Features only came after great reluctance if at all. I knew it needed to be rewritten, again.</p>

<p>This time around, I have a wider skillset than I did when I first started Jisho. I know a few more scripting languages reasonably well. Some <a href="http://www.python.org">I like a lot more</a> than <a href="http://www.php.net" title="Not a big fan">others</a>.</p>

<p>While I am still in the process of writing the parser, it&#8217;s coming together much quicker than the old versions ever did. More importantly, it doesn&#8217;t read like a bunch of voodoo magic. These tools excel for things like this. We should use them.</p>

<p>Today, Objective-C is still one of the most important hammers in my toolbox &#8211; perhaps even my favourite. But sometimes you need a saw.</p>

<p>As a software developer if you think something would be easier with a different tool, try it and find out. Best case scenerio you&#8217;re right and you&#8217;ve learned a new tool. Worst case scenario is you&#8217;ve learned a new tool and expanded your skillset. You can&#8217;t lose.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.james-vandyne.com/choose-the-right-tool-everything-is-not-a-nail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Universal Overlays Using UIViews on iOS</title>
		<link>http://www.james-vandyne.com/creating-universal-overlays-using-uiviews-on-ios/</link>
		<comments>http://www.james-vandyne.com/creating-universal-overlays-using-uiviews-on-ios/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 11:23:44 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.james-vandyne.com/?p=132</guid>
		<description><![CDATA[What happens when you are developing a UIViewController based application and you want to have an simple transparent overlay for displaying things, say a custom achievements UI in a game, or anything really. In my case, I had a singleton &#8230; <a href="http://www.james-vandyne.com/creating-universal-overlays-using-uiviews-on-ios/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>What happens when you are developing a UIViewController based application and you want to have an simple transparent overlay for displaying things, say a custom achievements UI in a game, or anything really.</p>

<p>In my case, I had a singleton that handles all interactions with GameCenter, called DAGameCenter. With this, anywhere in my application I could simply report an achievement with a single call:</p>

<pre class="code">
[[DAGameCenter sharedGameCenter] reportAchievement:@"com.mygame.achievement" 
                                 percent:100.0f];
</pre>

<p>DAGameCenter then tries to report the achievement to GameCenter and if it fails, archives it in a list of achievements to try reporting at another time. The proble comes in, as you probably guessed it, having a uniform method of displaying achievements to the user across all screens.</p>

<p>My first idea was to simply use a new UIWindow. UIWindow has properties for order the level of each window. I could simply create a new UIWindow, set the level to be above my keyWindow, and simply forward all events to my keyWindow so the user can still interact with the window below.</p>

<h3>Problem</h3>

<p>Apple advises against creating more than one UIWindow object. Your UIWindow is also already created for you by UIApplication when your app launches. So, things aren&#8217;t as simple as we would hope.</p>

<p>We can move up the object hierarchy a little bit to UIView, UIWindow&#8217;s parent class. We can simply put this view on top of our main view and all is peachy, right? Something like</p>

<pre class="code">
OverlayView *myOverlay = [[OverlayView alloc] initWithFrame:frame];
[[[UIApplication sharedApplication] keyWindow] addSubView:myOverlay];
</pre>

<p>Not quite&#8230;simply adding a UIView to our existing UIWindow causes our interface to become unresponsive. This is because our newly added view is capturing all events. As our view doesn&#8217;t handle any events, it simply forwards the event up the responder chain, to our UIWindow, which then forwards it to our UIApplication, and then the event is ignored.</p>

<p>Our mainView and  our overlayViews are not parent-child but they are both siblings of our UIWindow. This, in effect, creates our overlayView creates a second hierarchy of views in our application.</p>

<p>A quick and dirty solution would be to simply add a delegate to our overlayView and when creating our ourlayView set our mainView as the delegate. What happens when we push a second view above our mainView, say a settings screen. Our settings will become unresponsive unless we also remember to update our delegate.</p>

<p>So, this solution effectively loses maintainability after expanding beyond a single view and requires us to actively remember to set our delegates with navigation. It&#8217;s just asking for problems.</p>

<p>Our problems are two-fold: locating the view that is currently being displayed and then forwarding events to said view.</p>

<h3>Solution</h3>

<p>Forwarding events is quite simple. We simply need to overwrite the hitTest:withEvent method in our view.</p>

<p>It turns out that you can access a given view&#8217;s subviews with a simple call to&#8230;subviews. This returns an array, that&#8217;s helpfully ordered from back to front. UIView has a method that allows us to insert views above other views. UIWindow is a subclass of UIView, so all of this applies to it as well. We can gain access to an ordered array our subviews of our window anywhere in our application by simply calling subviews on our keyWindow.</p>

<p>As I&#8217;m sure you can probably guess by now, the solution. We simply need to insert our overlayView above our UIWindow. Then, we can forward events to the back-most subview that our window has.</p>

<p>When we push a modal view on to our UI to change screens from mainView to settings, it actually removes our mainView from our view hierarchy and replaces it with our settings view. This means that the &#8220;active&#8221; view will always be at 0.</p>

<p>All together, our code looks something like as follows:</p>

<pre class="code">

// Somewhere in our Singleton. Add our view so it is always above all views in our window
// In my case, I have it in the block that runs when a player is authenticated with GameCenter
-(void)insertAchievementView {
    UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
    if(!_achievementView)
        _achievementView = [[DAAchievementView alloc] initWithFrame:[mainWindow frame]];
    [mainWindow insertSubview:_achievementView aboveSubview:mainWindow];
}

// Then, in our actual achievementView, forward our hitTests
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] hitTest:point withEvent:event];
}
</pre>

<h3>Conclusion</h3>

<p>We can mimic the usage of a second UIWindow for displaying achievements of our application with a simple overlay view. Integrating the appropriate display methods of our overlay view in our singleton allows decoupling of the overlay and the view behind it, greatly reducing complexity. Furthermore, this decoupling allows all views in our application to utilize the overlay &#8220;for free&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.james-vandyne.com/creating-universal-overlays-using-uiviews-on-ios/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Improve Performance and Draw Your Own Strings on iPhone</title>
		<link>http://www.james-vandyne.com/improve-performance-and-draw-your-own-strings-on-iphone/</link>
		<comments>http://www.james-vandyne.com/improve-performance-and-draw-your-own-strings-on-iphone/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 11:16:34 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.james-vandyne.com/?p=20</guid>
		<description><![CDATA[Introduction Programmers are lazy. If you are going to have some no-frills text on the screen that does not change too often an UILabel is the way to go. Changing the is very simple. It&#8217;s just a matter of: myLabel.text &#8230; <a href="http://www.james-vandyne.com/improve-performance-and-draw-your-own-strings-on-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>

<p>Programmers are lazy. If you are going to have some no-frills text on the screen that does not change too often an UILabel is the way to go. Changing the is very simple. It&#8217;s just a matter of:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">myLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Change that text!&quot;</span>;</pre></div></div>


<p>What happens when you do this, every second for 45+ minutes (that&#8217;s 2700+ updates)?</p>

<h3>Problem</h3>

<p>I noticed that with every update of that label in the manner above my consumed ram was growing. Not a lot, but a few bytes. Enough though that after 2700+ times, it was causing some problems. I must be leaking I thought. Pop up Instruments with Leaks and test. No leakage apparent. Perhaps it was getting cached somewhere in UIKit?</p>

<p>As my application was going to be getting the proof-of-concept UI completely rewritten, UILabel was going to be a placeholder unless I could be a good lazy programmer and continue to use them.</p>

<p>However with UILabel seemingly cacheing the old labels or something, using them is out of the question.</p>

<h3>Solution</h3>

<p>Most programmers aren&#8217;t going to be updating their labels very often. Let alone 2700+ times every second.</p>

<p>My solution was a combination of Quartz2D (to change the color of the string) and NSString&#8217;s drawing categories.</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><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>
    <span style="color: #11740a; font-style: italic;">// Get the current context so we can draw.</span>
	CGContextRef context <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the fill color to white. </span>
	CGContextSetFillColorWithColor<span style="color: #002200;">&#40;</span>context, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span> CGColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>; 
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Draw in the specified rect</span>
	<span style="color: #002200;">&#91;</span>myString drawInRect<span style="color: #002200;">:</span>rect withFont<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;">16.0</span><span style="color: #002200;">&#93;</span>  lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeTailTruncation alignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Notes:</p>

<ul>
<li>This assumes that you have a property myString declared somewhere. </li>
<li>You could probably improve performance a bit by cacheing the UIFont. </li>
<li>By drawing our own strings we lose the convenience of having a CALayer ready to use.</li>
</ul>

<h3>Conclusion</h3>

<p>I used the above code (of course modified for posting)  in my application and poof my application was no longer growing. Moreover, it actually feels a bit snappier.</p>

<p>UILabel is great for displaying static or mostly static text on the screen. However if you are going to be updating it with any frequency, it is advantageous to draw text manually. UILabel uses the drawInRect: or drawAtPoint: methods to draw anyways, calling them directly saves your phone from a lot of unneeded calls. This equates to less execution faster which in turn means more battery life for your users. A win for everybody.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.james-vandyne.com/improve-performance-and-draw-your-own-strings-on-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

