<?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>zdima.net &#187; Objective-C</title>
	<atom:link href="http://zdima.net/blog/archives/tag/objective-c/feed" rel="self" type="application/rss+xml" />
	<link>http://zdima.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 22 May 2012 23:28:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>UITextField – A Complete API Overview</title>
		<link>http://zdima.net/blog/archives/14814</link>
		<comments>http://zdima.net/blog/archives/14814#comments</comments>
		<pubDate>Mon, 04 Jan 2010 17:58:18 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Contributors]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[UITextField]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=14814</guid>
		<description><![CDATA[The UITextField is probably one of the most commonly used UI controls on the iPhone. It is the primary method of user input via the keyboard and provides a great deal of additional functionality.
With the success of our las API tutorial on NSArray, I t...<p class="read-more"><a href="http://zdima.net/blog/archives/14814">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<p style="clear:both">The UITextField is probably one of the most commonly used UI controls on the iPhone. It is the primary method of user input via the keyboard and provides a great deal of additional functionality.</p>
<p>With the success of our las API tutorial on <a href="http://icodeblog.com/2009/08/26/objective-c-tutorial-nsarray/">NSArray</a>, I thought I would do another walkthrough, this time on UITextField. I will be explaining all of the properties for it as well as bringing up some functionality that you may not have known about.</p>
<h2>Text Attributes</h2>
<p style="clear:both">The attributes have to do with the actual text inside of the UITextField.</p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>text</td>
<td>The text displayed in the UITextField</td>
</tr>
<tr>
<td>placeholder</td>
<td>The text that gets displayed prior to the user entering in anything. This text is usually a lighter color than the primary text to denote that it will be replaced.</td>
</tr>
<tr>
<td>font</td>
<td>The font of the text to be displayed. You can set it like this</td>
</tr>
<tr>
<td>textColor</td>
<td>The color of the text that is displayed</td>
</tr>
<tr>
<td>textAlignment</td>
<td>How the text is aligned in the UITextField. The possible values for this are UITextAlignmentLeft, UITextAlignmentRight, UITextAlignmentCenter</td>
</tr>
</tbody>
</table>
<p style="clear:both">
<p style="clear:both">Here are some examples of using these properties.</p>
<p style="clear:both">
<div>
<div>
<pre style="font-family:monospace"><span style="color:#11740a;font-style:italic">// Setting the text</span>
<span style="color:#002200">[</span>myTextField setText<span style="color:#002200">:</span><span style="color:#bf1d1a">@</span><span style="color:#bf1d1a">&quot;This is some text!&quot;</span><span style="color:#002200">]</span>;
 
<span style="color:#11740a;font-style:italic">// Setting the placeholder</span>
<span style="color:#002200">[</span>myTextField setPlaceholder<span style="color:#002200">:</span><span style="color:#bf1d1a">@</span><span style="color:#bf1d1a">&quot;Type text here&quot;</span><span style="color:#002200">]</span>;
 
<span style="color:#11740a;font-style:italic">// Setting the font.</span>
<span style="color:#002200">[</span>myTextField setFont<span style="color:#002200">:</span><span style="color:#002200">[</span>UIFont fontWithName<span style="color:#002200">:</span><span style="color:#bf1d1a">@</span><span style="color:#bf1d1a">&quot;Times New Roman&quot;</span> size<span style="color:#002200">:</span><span style="color:#2400d9">14</span><span style="color:#002200">]</span><span style="color:#002200">]</span>;
 
<span style="color:#11740a;font-style:italic">// Setting the text color</span>
<span style="color:#002200">[</span>myTextField setTextColor<span style="color:#002200">:</span><span style="color:#002200">[</span>UIColor blueColor<span style="color:#002200">]</span><span style="color:#002200">]</span>;
 
<span style="color:#11740a;font-style:italic">// Setting the text alignment</span>
<span style="color:#002200">[</span>myTextField setTextAlignment<span style="color:#002200">:</span>UITextAlignmentCenter<span style="color:#002200">]</span>;</pre>
</div>
</div>
<p style="clear:both">
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" src="http://icodeblog.com/wp-content/uploads/2009/12/screenshot_01.png" alt="" width="298" height="50" align="left">Here is what the UITextField would look like after we update these properties.</p>
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_02" src="http://icodeblog.com/wp-content/uploads/2009/12/screenshot_02.png" alt="" width="298" height="52" align="left"><br style="clear:both"></p>
<h2>Adjusting the size of the text in the UITextField</h2>
<p style="clear:both">
<p style="clear:both">
<p style="clear:both">The text displayed in our UITextField can be dynamically sized based on the width of the UITextField. The benefit of this is all of the text being typed will be visible on the screen. It will shrink the text down until it reaches the default font size of 17. So, for this to make sense, you must set the font size of the UITextField to something larger than 17.</p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>adjustsFontSizeToFitWidth</td>
<td>Boolean value denoting whether to fit the font size to the width of the UITextField.</td>
</tr>
</tbody>
</table>
<p style="clear:both">
<p style="clear:both">Here is an example of using these properties.</p>
<p style="clear:both">
<div>
<div>
<pre style="font-family:monospace"><span style="color:#002200">[</span>myTextField setFont<span style="color:#002200">:</span><span style="color:#002200">[</span>UIFont fontWithName<span style="color:#002200">:</span><span style="color:#bf1d1a">@</span><span style="color:#bf1d1a">&quot;Times New Roman&quot;</span> size<span style="color:#002200">:</span><span style="color:#2400d9">30</span><span style="color:#002200">]</span><span style="color:#002200">]</span>;
<span style="color:#002200">[</span>myTextField setAdjustsFontSizeToFitWidth<span style="color:#002200">:</span><span style="color:#a61390">YES</span><span style="color:#002200">]</span>;</pre>
</div>
</div>
<p style="clear:both">
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_03" src="http://icodeblog.com/wp-content/uploads/2009/12/screenshot_03.png" alt="" width="298" height="55">Here are some screenshots of the text shrinking when typing in the UITextField.</p>
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_04" src="http://icodeblog.com/wp-content/uploads/2009/12/screenshot_04.png" alt="" width="298" height="48"></p>
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_05" src="http://icodeblog.com/wp-content/uploads/2009/12/screenshot_05.png" alt="" width="298" height="52"><br style="clear:both"></p>
<h2>Managing the editor’s behavior</h2>
<p style="clear:both">
<p style="clear:both">
<p style="clear:both">These two properties are pretty straight forward.</p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>editing</td>
<td>Read-only boolean value letting you know if the user is currently editing the UITextField</td>
</tr>
<tr>
<td>clearsOnBeginEditing</td>
<td>Clears the text in the field every time the user begins to edit it.</td>
</tr>
</tbody>
</table>
<p style="clear:both">
<p style="clear:both">Not very exciting and probably doesn’t even deserve an example…</p>
<p style="clear:both">
<p style="clear:both">
<p style="clear:both">
<h2>Setting the view’s background appearance</h2>
<p style="clear:both">
<p style="clear:both">
<p style="clear:both">This group of properties defines how the UITextField will look. If you have ever seen a fancy input box, this is how they are doing it.</p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>borderStyle</td>
<td>Defines the type of border for the UITextField. Possible choices are UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, and UITextBorderStyleRoundedRect. The default is UITextBorderStyleNone.</td>
</tr>
<tr>
<td>background</td>
<td>A UIImage representing the background image of the UITextField when it’s enabled. If this field is altered the borderStyle property is ignored.</td>
</tr>
<tr>
<td>backgroundDisabled</td>
<td>A UIImage representing the background image of the UITextField when it’s disabled.</td>
</tr>
</tbody>
</table>
<p style="clear:both">
<p style="clear:both">Here is are some example of using each of the border styles</p>
<p style="clear:both">
<div>
<div>
<pre style="font-family:monospace"><span style="color:#11740a;font-style:italic">// Border Style None</span>
<span style="color:#002200">[</span>myTextField setBorderStyle<span style="color:#002200">:</span>UITextBorderStyleNone<span style="color:#002200">]</span>;</pre>
</div>
</div>
<p style="clear:both">
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_05" src="http://icodeblog.com/wp-content/uploads/2010/01/screenshot_05.png" alt="" width="290" height="43" align="left"></p>
<div>
<div>
<pre style="font-family:monospace"><span style="color:#11740a;font-style:italic">// Border Style Line</span>
<span style="color:#002200">[</span>myTextField setBorderStyle<span style="color:#002200">:</span>UITextBorderStyleLine<span style="color:#002200">]</span>;</pre>
</div>
</div>
<p style="clear:both">
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_03" src="http://icodeblog.com/wp-content/uploads/2010/01/screenshot_03.png" alt="" width="288" height="41" align="left"></p>
<div>
<div>
<pre style="font-family:monospace"><span style="color:#11740a;font-style:italic">// Border Style Bezel</span>
<span style="color:#002200">[</span>myTextField setBorderStyle<span style="color:#002200">:</span>UITextBorderStyleBezel<span style="color:#002200">]</span>;</pre>
</div>
</div>
<p style="clear:both">
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_02" src="http://icodeblog.com/wp-content/uploads/2010/01/screenshot_02.png" alt="" width="291" height="42" align="left"></p>
<div>
<div>
<pre style="font-family:monospace"><span style="color:#11740a;font-style:italic">// Border Style Rounded Rect</span>
<span style="color:#002200">[</span>myTextField setBorderStyle<span style="color:#002200">:</span>UITextBorderStyleRoundedRect<span style="color:#002200">]</span>;</pre>
</div>
</div>
<p style="clear:both">
<p style="clear:both"><a href="http://icodeblog.com/wp-content/uploads/2010/01/screenshot_04.png"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_04" src="http://icodeblog.com/wp-content/uploads/2010/01/screenshot_04.png" alt="" width="288" height="41" align="left"></a></p>
<p style="clear:both"><a href="http://icodeblog.com/wp-content/uploads/2010/01/bg.png"><img style="display:inline;float:left;margin:0 10px 10px 0" title="bg" src="http://icodeblog.com/wp-content/uploads/2010/01/bg.png" alt="" width="185" height="28" align="left"></a>The border style is not terribly exciting. However, you can really spruce up your UITextFields using the background property. Here is an example of setting the background property to this image.</p>
<p style="clear:both">
<div>
<div>
<pre style="font-family:monospace">myTextField.textAlignment <span style="color:#002200">=</span> UITextAlignmentCenter;
myTextField.textColor <span style="color:#002200">=</span> <span style="color:#002200">[</span>UIColor whiteColor<span style="color:#002200">]</span>;
myTextField.borderStyle <span style="color:#002200">=</span> UITextBorderStyleNone;
myTextField.background <span style="color:#002200">=</span> <span style="color:#002200">[</span>UIImage imageNamed<span style="color:#002200">:</span><span style="color:#bf1d1a">@</span><span style="color:#bf1d1a">&quot;bg.png&quot;</span><span style="color:#002200">]</span>;</pre>
</div>
</div>
<p style="clear:both">
<p style="clear:both"><img style="display:inline;float:left;margin:0 10px 10px 0" title="screenshot_01" src="http://icodeblog.com/wp-content/uploads/2010/01/screenshot_01.png" alt="" width="288" height="46" align="left">and the result… Looks pretty good ehh? One GOTCHA that I want to point out here is, to get the background property to work correctly, you must set the boderStyle to anything other than UITextBorderStyleRoundedRect. Otherwise, the default UITextField will be displayed.<br />
Setting the view’s background appearance</p>
<p style="clear:both">
<h2>Managing Overlay Views</h2>
<p>Another interesting way of customizing your UITextFields is to use an overlay. UITextField offers a left and right overlay for your UITextFields. Here are the properties:</p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>clearButtonMode</td>
<td>The circled X that gets displayed when typing. Used to clear out the text. Possible values: UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways</td>
</tr>
<tr>
<td>leftView</td>
<td>The view that appears to the left inside a UITextField. This could be something like a magnifying glass for search.</td>
</tr>
<tr>
<td>leftViewMode</td>
<td>Works like clearButtonMode, but toggles the leftView.</td>
</tr>
<tr>
<td>rightView</td>
<td>Same as leftView, except it aligns to the right.</td>
</tr>
<tr>
<td>rightViewMode</td>
<td>Same as leftViewMode but controls the rightView</td>
</tr>
</tbody>
</table>
<p>Let’s take a look at how adjusting the leftView works:</p>
<div>
<div>
<pre style="font-family:monospace">UIImageView <span style="color:#002200">*</span> myView <span style="color:#002200">=</span> <span style="color:#002200">[</span><span style="color:#002200">[</span> UIImageView  alloc <span style="color:#002200">]</span>  initWithImage <span style="color:#002200">:</span>
		<span style="color:#002200">[</span>UIImage  imageNamed <span style="color:#002200">:</span> <span style="color:#bf1d1a">@</span><span style="color:#bf1d1a">&quot;wordpress.png&quot;</span> <span style="color:#002200">]</span><span style="color:#002200">]</span>;
<span style="color:#002200">[</span>myTextField  setLeftView <span style="color:#002200">:</span>myView<span style="color:#002200">]</span>;
<span style="color:#002200">[</span> myTextField   setLeftViewMode<span style="color:#002200">:</span> UITextFieldViewModeAlways<span style="color:#002200">]</span>;
<span style="color:#002200">[</span>myView release <span style="color:#002200">]</span>;</pre>
</div>
</div>
<p><img title="screenshot_01" src="http://icodeblog.com/wp-content/uploads/2010/01/screenshot_011.png" alt="" width="291" height="44"></p>
<p>As you can see, the text aligns after the image.  This is a very simple way to really spruce up your UITextFields.</p>
<p>The last thing we are going to discuss is showing and hiding the keyboard.</p>
<h2>Showing and Hiding The Keyboard</h2>
<p>To show the keyboard:</p>
<div>
<div>
<pre style="font-family:monospace"><span style="color:#002200">[</span>myTextField becomeFirstResponder<span style="color:#002200">]</span>;</pre>
</div>
</div>
<p>To hide the keyboard</p>
<div>
<div>
<pre style="font-family:monospace"><span style="color:#002200">[</span>myTextField resignFirstResponder<span style="color:#002200">]</span>;</pre>
</div>
</div>
<p>Well, I hope you have enjoyed this tutorial on the <strong>UITextField</strong>.  I would love to see links to some interesting custom UITextFields in the comments, so please post them.  Thanks for reading and happy iCoding!</p>
<div style="clear:both"></div>
<p><img src="http://feeds.feedburner.com/~r/icodeblog/~4/kITvH_4Mwp0" height="1" width="1"></p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p></p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/14814/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Specifying Simulator Only Code</title>
		<link>http://zdima.net/blog/archives/12866</link>
		<comments>http://zdima.net/blog/archives/12866#comments</comments>
		<pubDate>Wed, 15 Apr 2009 03:28:47 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Contributors]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=12866</guid>
		<description><![CDATA[There are times when you need to write code that is only applicable when working with the simulator. 
As an example, I was recently working with an application that required the user to take a picture with their iPhone. Once the picture was taken the a...<p class="read-more"><a href="http://zdima.net/blog/archives/12866">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<p>There are times when you need to write code that is only applicable when working with the simulator. </p>
<p>As an example, I was recently working with an application that required the user to take a picture with their iPhone. Once the picture was taken the application was to show the image to the user, at which point they would measure the size of an object in the image using various controls (movable lines) and a reference object in the image. Problem is, the simulator does not have much for camera functionality (obviously, there is no lens) so any picture taken shows up as a black image.<br />
<span></span></p>
<p>To get around this problem, I used the compiler directive below and inside the simulator section I would load an image and use the same throughout the application as if it was created from the camera.</p>
<div>
<div>
<pre ><span >#if TARGET_IPHONE_SIMULATOR</span>
  NSLog<span >(</span><span >@</span><span >&quot;Running on Simulator&quot;</span><span >)</span>;
<span >#else</span>
  NSLog<span >(</span><span >@</span><span >&quot;Running on Device&quot;</span><span >)</span>;
<span >#endif</span></pre>
</div>
</div>
<p>Working with a directive such as this is really handy as you can move between testing with the simulator and a real device without having to change any code.</p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/12866/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Of BOOL and YES</title>
		<link>http://zdima.net/blog/archives/12837</link>
		<comments>http://zdima.net/blog/archives/12837#comments</comments>
		<pubDate>Wed, 31 Dec 2008 19:28:56 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Contributors]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=12837</guid>
		<description><![CDATA[It may not be immediately clear that the Objective C BOOL &#34;type&#34; is not actually a boolean type at all. This is a legacy from the original C language, which does not have an intrinsic boolean type (the iPhone GCC C compiler supports the ISO C99 standard which does define a bool type). To clarify, [...] <p class="read-more"><a href="http://zdima.net/blog/archives/12837">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<p>It may not be immediately clear that the Objective C BOOL &quot;type&quot; is not actually a boolean type at all. This is a legacy from the original C language, which does not have an intrinsic boolean type (the iPhone GCC C compiler supports the ISO C99 standard which does define a bool type). To clarify, [...]</p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/12837/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class Variables</title>
		<link>http://zdima.net/blog/archives/12843</link>
		<comments>http://zdima.net/blog/archives/12843#comments</comments>
		<pubDate>Wed, 10 Dec 2008 11:04:03 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Contributors]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=12843</guid>
		<description><![CDATA[I previously wrote about the lack of support for private methods when working with Objective-C. As part of that post I presented a few work-arounds. Along the same lines, there are is no support for class variables in Objective-C. This post will explore this a little further and walk through a short example that shows [...] <p class="read-more"><a href="http://zdima.net/blog/archives/12843">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<p>I previously wrote about the lack of support for private methods when working with Objective-C. As part of that post I presented a few work-arounds. Along the same lines, there are is no support for class variables in Objective-C. This post will explore this a little further and walk through a short example that shows [...]</p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/12843/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C Object as a C Structure</title>
		<link>http://zdima.net/blog/archives/12844</link>
		<comments>http://zdima.net/blog/archives/12844#comments</comments>
		<pubDate>Mon, 08 Dec 2008 07:26:23 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Contributors]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=12844</guid>
		<description><![CDATA[Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, it’s interesting none-the-less. 
In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure [...] <p class="read-more"><a href="http://zdima.net/blog/archives/12844">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<p>Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, it’s interesting none-the-less.<br />
In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure [...]</p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/12844/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

