<?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>iPhone Programming Tutorials &#187; UITextField</title>
	<atom:link href="/tag/uitextfield/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>Conquering the mobile universe</description>
	<lastBuildDate>Wed, 31 Aug 2011 22:13:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>iPhone Coding Snippet: Live Character Counter, Word filter, and 1337 Translator For A UITextField</title>
		<link>http://icodeblog.com/2010/01/06/live-character-counter-for-a-uitextfield/</link>
		<comments>http://icodeblog.com/2010/01/06/live-character-counter-for-a-uitextfield/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 18:46:57 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[1337]]></category>
		<category><![CDATA[Editing Changed]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[UITextField]]></category>
		<category><![CDATA[Word filter]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1686</guid>
		<description><![CDATA[<p>I'm sure you have seen a Twitter client such as <a href="http://stone.com/Twittelator/index.html">Twittelator Pro</a> or <a href="http://www.atebits.com/tweetie-iphone/">Tweetie 2</a> count down the characters as you type your Tweet.  This is done by responding to the Editing Changed action on a UITextField.</p>  

<p>Today, I will show you how you can implement this functionality in your application as well as show you some other real world examples of responding to this action.  Here are a few of the uses that we will discuss:</p>
<ul>
	<li>Countdown of characters allowed - Used when the user is limited by a certain number of characters</li>
	<li>Word filter - useful in filtering out swear words or other unwanted text</li>
	<li>Live translator - our example will translate english to <a href="http://en.wikipedia.org/wiki/Leet">1337</a> :)</li>
</ul>
<p>I will create the core code and show you how to modify it slightly to implement the other 2 applications.</p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure you have seen a Twitter client such as <a href="http://stone.com/Twittelator/index.html">Twittelator Pro</a> or <a href="http://www.atebits.com/tweetie-iphone/">Tweetie 2</a> count down the characters as you type your Tweet.  This is done by responding to the Editing Changed action on a UITextField.  Today, I will show you how you can implement this functionality in your application as well as show you some other real world examples of responding to this action.  Here are a few of the uses that we will discuss:</p>
<ul>
<li>Countdown of characters allowed &#8211; Used when the user is limited by a certain number of characters</li>
<li>Word filter &#8211; useful in filtering out swear words or other unwanted text</li>
<li>Live translator &#8211; our example will translate english to <a href="http://en.wikipedia.org/wiki/Leet">1337</a> <img src='/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>I will create the core code and show you how to modify it slightly to implement the other 2 applications.</p>
<p>Let&#8217;s begin by creating the countdown application.</p>
<h2>1. Start With A View Based Application</h2>
<p>Name the application TextMonitor.  I&#8217;m giving it this generic name since it will become one of 3 applications.</p>
<div id="attachment_1688" class="wp-caption alignnone" style="width: 610px"><a href="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_0121.png"><img class="size-full wp-image-1688" title="screenshot_01" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_0121.png" alt="" width="600" height="442" /></a><p class="wp-caption-text">View based application </p></div>
<h2>2. Add You IBOutlets And IBActions</h2>
<p>Before creating my User Interfaces, I always like to declare the  IBOutles and IBActions first.  That way, I only need to open Interface Builder one time to add the UI components and connect them up.  Open TextMonitorViewController.h and add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TextMonitorViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&#123;</span>
	IBOutlet UITextView <span style="color: #002200;">*</span> liveOutputTextView;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITextView <span style="color: #002200;">*</span> liveOutputTextView;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>textFieldDidUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Pretty straight forward.  We first declare an IBOutlet for the UITextView that we will be updating.  It will be used to show our characters remaining.  Next, we declare an IBAction that will be called every time the user types a character in the UITextField.  Now that our properties and actions have been set up, it&#8217;s time to create our interface.</p>
<h2>3. Creating The Interface</h2>
<p>Open up the file TextMonitorViewController.xib in Interface Builder.  Drag a UITextField and a UITextView on to the main view.  It should look something like this (I change the colors a bit).</p>
<div id="attachment_1689" class="wp-caption alignnone" style="width: 330px"><a href="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_0131.png"><img class="size-full wp-image-1689" title="screenshot_01" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_0131.png" alt="" width="320" height="502" /></a><p class="wp-caption-text">Main View</p></div>
<p>Once you have created the view, connect the UITextView to the liveOutputTextView IBOutlet of the File&#8217;s Owner object. To do this click on &#8220;File&#8217;s Owner&#8221; in the window titled &#8220;TextMonitorViewController.xib&#8221;, the click Tools -&gt; Connections Inspector.  Drag from the circle that sais liveOutputTextView to your UITextView on the screen and release.</p>
<p>Next, you will need to connect the textFieldDidUpdate IBOutlet to the Editing Changed action of the UITextField.  Click on the UITextField and open the Connection Inspector.  Click and drag from the Editing Changed action to the File&#8217;s Owner object.  The method ﻿textFieldDidUpdate should appear, mouse over it and release the click.</p>
<p>The connections inspector window of File&#8217;s Owner should now look like this.</p>
<div id="attachment_1691" class="wp-caption alignnone" style="width: 297px"><a href="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_0211.png"><img class="size-full wp-image-1691" title="screenshot_02" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_0211.png" alt="" width="287" height="195" /></a><p class="wp-caption-text">Connections Inspector</p></div>
<p>You may now close Interface Builder and return to the code.  The next thing we will do is implement the textFieldDidUpdate method.</p>
<h2>4. The textFieldDidUpdate Method</h2>
<p>This is the method where we will put all of our code.  Open up TextMonitorViewController.m and add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;TextMonitorViewController.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> TextMonitorViewController
&nbsp;
<span style="color: #a61390;">@synthesize</span> liveOutputTextView;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> textFieldDidUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
	UITextField <span style="color: #002200;">*</span> textField <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> sender;
	<span style="color: #a61390;">int</span> maxChars <span style="color: #002200;">=</span> <span style="color: #2400d9;">140</span>;
	<span style="color: #a61390;">int</span> charsLeft <span style="color: #002200;">=</span> maxChars <span style="color: #002200;">-</span> <span style="color: #002200;">&#91;</span>textField.text length<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>charsLeft <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		UIAlertView <span style="color: #002200;">*</span> alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;No more characters&quot;</span>
			message<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;You have reached the character limit of %d.&quot;</span>,maxChars<span style="color: #002200;">&#93;</span>
			delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>
			 cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span>
			otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	liveOutputTextView.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d characters remaining.&quot;</span>,charsLeft<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidUnload <span style="color: #002200;">&#123;</span>
	liveOutputTextView <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>liveOutputTextView release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The first thing that must be done is to synthesize the liveOutputTextView property.  That should be pretty obvious. Let&#8217;s examine the code for the textFieldDidUpdate method.</p>
<p>First, we cast the sender object to a UITextField.  This is because we know that a UITextField is invoking this method and passing in a reference to itself.  Next, we specify the maximum number of characters allowed (I used the Twitter standard of 140).  Following this we simply see how many characters the user has left by subtracting the string length of the user input from the maximum characters allowed.  If you wanted to simply count the characters that the user typed, you would not need this additional math, you could simple output [textField.text length].</p>
<p>I have put in a check to see if the user has typed all 140 characters.  If so, I display a message to them notifying them that they don&#8217;t have any remaining characters.  At this point, I chose not to implement the code to prevent them from typing when they reach the limit (I&#8217;ll leave that as a challenge to you) as most Twitter clients allow you to go over the limit and post to <a href="http://www.twitlonger.com/">twitlonger</a>.</p>
<p>Finally, we update the UITextView to notify the user how many characters they have already typed.  The final product should look something like this.</p>
<p><a href="http://staging.icodeblog.com/wp-content/uploads/2010/01/Screen-shot-2010-01-06-at-10.49.23-AM1.png"><img class="alignnone size-full wp-image-1693" title="Screen shot 2010-01-06 at 10.49.23 AM" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/Screen-shot-2010-01-06-at-10.49.23-AM1.png" alt="" width="414" height="770" /></a></p>
<p>Now that you have seen how to update a UITextView in real time, I will show you how we could alter our textFieldDidUpdate method, to change the program entirely.</p>
<h2>Creating a word filter</h2>
<p>Our word filter will be pretty simple.  We will create an NSDictionary where the keys will be swear words and the values will be their replacements.  We will then enumerate over the keys of the dictionary and replace them in display text with their values.  To do this, replace the code in textFieldDidUpdate with the following code.</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>IBAction<span style="color: #002200;">&#41;</span> textFieldDidUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
	UITextField <span style="color: #002200;">*</span> textField <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> sender;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span> filteredWords <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
			 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fecal matter&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;poop&quot;</span>,
			 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;patooti&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;butt&quot;</span>,
			 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Worst band ever&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Nickelback&quot;</span>,
			 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;nice person&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;douche bag&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> newString <span style="color: #002200;">=</span> textField.text;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> naughtyWord <span style="color: #a61390;">in</span> filteredWords.allKeys<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> goodWord <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>filteredWords objectForKey<span style="color: #002200;">:</span>naughtyWord<span style="color: #002200;">&#93;</span>;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;replacing %@ &quot;</span>,naughtyWord<span style="color: #002200;">&#41;</span>;
		newString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newString stringByReplacingOccurrencesOfString<span style="color: #002200;">:</span>naughtyWord
			withString<span style="color: #002200;">:</span>goodWord<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	liveOutputTextView.text <span style="color: #002200;">=</span> newString;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The result is actually quite amusing.  Check out the screenshot below. iCodeBlog is a family friendly iPhone development site <img src='/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://staging.icodeblog.com/wp-content/uploads/2010/01/Screen-shot-2010-01-06-at-11.02.45-AM1.png"><img class="alignnone size-full wp-image-1694" title="Screen shot 2010-01-06 at 11.02.45 AM" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/Screen-shot-2010-01-06-at-11.02.45-AM1.png" alt="" width="414" height="770" /></a></p>
<h2>Creating a 1337 Translator</h2>
<p>We can now apply this same concept to create a 1337 translator.  The translator will translate the characters as the user types into 1337 speak. (Only the 133737 0f h4x0r5 5p34k 1337).  Replace the code in textFieldDidUpdate with the code below to create your translator.</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>IBAction<span style="color: #002200;">&#41;</span> textFieldDidUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
	UITextField <span style="color: #002200;">*</span> textField <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> sender;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span> l337 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;4&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;|3&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;3&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;e&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;i&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;l&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;0&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;o&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;|2&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;r&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;5&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;s&quot;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;7&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;t&quot;</span>,
						   <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> newString <span style="color: #002200;">=</span> textField.text;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> word <span style="color: #a61390;">in</span> l337.allKeys<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> h4x <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>l337 objectForKey<span style="color: #002200;">:</span>word<span style="color: #002200;">&#93;</span>;
		newString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newString stringByReplacingOccurrencesOfString<span style="color: #002200;">:</span>word withString<span style="color: #002200;">:</span>h4x<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	liveOutputTextView.text <span style="color: #002200;">=</span> newString;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For those of you who don&#8217;t know what 1337 is, <a href="http://en.wikipedia.org/wiki/Leet">read up on it</a>.  Check out the result that it produces.</p>
<p><a href="http://staging.icodeblog.com/wp-content/uploads/2010/01/Screen-shot-2010-01-06-at-11.21.15-AM1.png"><img class="alignnone size-full wp-image-1699" title="Screen shot 2010-01-06 at 11.21.15 AM" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/Screen-shot-2010-01-06-at-11.21.15-AM1.png" alt="" width="414" height="770" /></a></p>
<p>This translator is actually pretty stupid, but you can see how you could use this to do all sorts of other translations.  A good programming challenge for you could be to make a translator that does base 10 to binary.</p>
<p>So, that concludes our tutorial on live updates based on the user input.  I&#8217;m sure we will be seeing a whole slew of terrible character counters, word filters, and 1337 translators in the app store after this.  Send me the links so that I may partake in the making fun <img src='/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://twitter.com/brandontreb">Follow me on Twitter</a></p>
<p>Download the source for this tutorial here <a href="http://staging.icodeblog.com/wp-content/uploads/2010/01/TextMonitor1.zip">TextMonitor.zip</a></p>
<p>Until next time, happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/01/06/live-character-counter-for-a-uitextfield/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>UITextField – A Complete API Overview</title>
		<link>http://icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/</link>
		<comments>http://icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 17:58:18 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[UITextField]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1547</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 <a href="/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.
Text Attributes
The  ...]]></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="/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.<br />
<style type="text/css"> table.api td {border:1px solid #777;} </style>
</p>
<table class="api" 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 class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting the text</span>
<span style="color: #002200;">&#91;</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;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the placeholder</span>
<span style="color: #002200;">&#91;</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;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the font.</span>
<span style="color: #002200;">&#91;</span>myTextField setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Times New Roman&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">14</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the text color</span>
<span style="color: #002200;">&#91;</span>myTextField setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the text alignment</span>
<span style="color: #002200;">&#91;</span>myTextField setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1570" style="display: inline; float: left; margin: 0 10px 10px 0;" src="http://staging.icodeblog.com/wp-content/uploads/2009/12/screenshot_011.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 class="alignnone size-full wp-image-1571" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_02" src="http://staging.icodeblog.com/wp-content/uploads/2009/12/screenshot_021.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 class="api" 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 class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>myTextField setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Times New Roman&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">30</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>myTextField setAdjustsFontSizeToFitWidth<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1577" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_03" src="http://staging.icodeblog.com/wp-content/uploads/2009/12/screenshot_031.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 class="alignnone size-full wp-image-1578" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_04" src="http://staging.icodeblog.com/wp-content/uploads/2009/12/screenshot_041.png" alt="" width="298" height="48" /></p>
<p style="clear: both;"><img class="alignnone size-full wp-image-1579" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_05" src="http://staging.icodeblog.com/wp-content/uploads/2009/12/screenshot_051.png" alt="" width="298" height="52"  /><br style="clear: both;" /></p>
<h2>Managing the editor&#8217;s behavior</h2>
<p style="clear: both;">
<p style="clear: both;">
<p style="clear: both;">These two properties are pretty straight forward.</p>
<table class="api" 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&#8217;t even deserve an example&#8230;</p>
<p style="clear: both;">
<p style="clear: both;">
<p style="clear: both;">
<h2>Setting the view&#8217;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  class="api" 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&#8217;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&#8217;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 class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style None</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleNone<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1635" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_05" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_051.png" alt="" width="290" height="43" align="left" /></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style Line</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleLine<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1637" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_03" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_031.png" alt="" width="288" height="41" align="left" /></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style Bezel</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleBezel<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1636" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_02" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_022.png" alt="" width="291" height="42" align="left" /></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style Rounded Rect</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleRoundedRect<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p style="clear: both;">
<p style="clear: both;"><a class="image-link" href="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_041.png"><img class="alignnone size-full wp-image-1638" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_04" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_041.png" alt="" width="288" height="41" align="left" /></a></p>
<p style="clear: both;"><a class="image-link" href="http://staging.icodeblog.com/wp-content/uploads/2010/01/bg1.png"><img class="alignnone size-full wp-image-1641" style="display: inline; float: left; margin: 0 10px 10px 0;" title="bg" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/bg1.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 class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">myTextField.textAlignment <span style="color: #002200;">=</span> UITextAlignmentCenter;
myTextField.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
myTextField.borderStyle <span style="color: #002200;">=</span> UITextBorderStyleNone;
myTextField.background <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bg.png&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1642" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_01" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_015.png" alt="" width="288" height="46" align="left" />and the result&#8230; 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&#8217;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 class="api" 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&#8217;s take a look at how adjusting the leftView works:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImageView <span style="color: #002200;">*</span> myView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span> UIImageView  alloc <span style="color: #002200;">&#93;</span>  initWithImage <span style="color: #002200;">:</span>
		<span style="color: #002200;">&#91;</span>UIImage  imageNamed <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;wordpress.png&quot;</span> <span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>myTextField  setLeftView <span style="color: #002200;">:</span>myView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span> myTextField   setLeftViewMode<span style="color: #002200;">:</span> UITextFieldViewModeAlways<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>myView release <span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><img class="alignnone size-full wp-image-1651" title="screenshot_01" src="http://staging.icodeblog.com/wp-content/uploads/2010/01/screenshot_0111.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 class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>myTextField becomeFirstResponder<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>To hide the keyboard</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>myTextField resignFirstResponder<span style="color: #002200;">&#93;</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>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
