<?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>Tupil Code Blog &#187; iPhone</title>
	<atom:link href="http://blog.tupil.com/tag/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tupil.com</link>
	<description>(Get up early, code often)</description>
	<lastBuildDate>Fri, 27 Aug 2010 10:50:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Adding Hyphenation to NSString</title>
		<link>http://blog.tupil.com/adding-hyphenation-to-nsstring/</link>
		<comments>http://blog.tupil.com/adding-hyphenation-to-nsstring/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 19:21:19 +0000</pubDate>
		<dc:creator>Eelco Lempsink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Hunspell]]></category>
		<category><![CDATA[Hyphenation]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Justification]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[TouchXML]]></category>
		<category><![CDATA[Typesetting]]></category>

		<guid isPermaLink="false">http://blog.tupil.com/?p=120</guid>
		<description><![CDATA[Khoi Vinh recently showed that the typesetting in Apple&#8217;s iBooks is quite horrendous. One obvious problem is that the text is layout with justification (which is probably an appropriate decision when typesetting books), but lacks hyphenation. John Gruber does not approve. The fact is, there are pretty good algorithms for hyphenation.  The Hunspell project has a library that [...]]]></description>
			<content:encoded><![CDATA[<p>Khoi Vinh recently showed that the typesetting in Apple&#8217;s iBooks is <a href="http://www.subtraction.com/2010/06/08/better-screen-same-typography">quite horrendous</a>. One obvious problem is that the text is layout with justification (which is probably an appropriate decision when typesetting books), but lacks hyphenation. <a href="http://daringfireball.net/linked/2010/06/09/powazek-reader">John Gruber does not approve.</a></p>
<p>The fact is, there are pretty good algorithms for hyphenation.  The <a href="http://hunspell.sourceforge.net/">Hunspell</a> project has a library that powers, among other projects, OpenOffice.org and extends the algorithm that was implemented for TeX long ago.  Time to bring some of that goodness to Cocoa!</p>
<p>I implemented a simple category for NSString to add <a href="http://www.fileformat.info/info/unicode/char/ad/index.htm">UTF-8 soft hyphen</a>s to a string. In this post I show how to use it in your project, including some examples.</p>
<h2>Setup</h2>
<p>First, gather all required files:</p>
<ul>
<li>Get <a href="http://github.com/tupil/hyphenate">NSString+Hyphenate from GitHub</a></li>
<li>Download the <a href="http://sourceforge.net/projects/hunspell/files/Hyphen/">Hyphen library</a></li>
<li>The Hyphen library contains the hyphenation dictionary for US English but if you want to support more languages you can download more <a href="http://wiki.services.openoffice.org/wiki/Dictionaries">dictionaries from OpenOffice.org</a></li>
</ul>
<p>Second, after unzipping put the code in place.</p>
<ul>
<li>Add the <code>NSString+Hyphenate .h</code> and <code>.m</code> file to your project.</li>
<li>To statically add the hyphen library to your project add the <code>hyphen.h</code>, <code>hyphen.c</code> and <code>hnjalloc.h</code>, <code>hnjalloc.c</code> files.</li>
</ul>
<p>Third, and finally, add the <code>.dic</code> files to the <code>Hyphenate.bundle</code> and add the bundle to your project. Your project source tree should now contain all the necessary files and look something like this.</p>
<p><img class="alignnone size-full wp-image-128" title="Hyphenate - Adding files" src="http://blog.tupil.com/wp-content/uploads/2010/06/hyphenate_adding_files.png" alt="" width="264" height="365" /></p>
<h2>Usage</h2>
<p>The <code>Hyphenate</code> category gives you one method: <code>-stringByHyphenatingWithLocale:</code>. Its usage is straightforward:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;It was in the fourth year of my apprenticeship to Joe, and it was a Saturday night.&quot;</span>;
    <span style="color: #400080;">NSLocale</span><span style="color: #002200;">*</span> en <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;en_US&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> hyphenated <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>text stringByHyphenatingWithLocale<span style="color: #002200;">:</span>en<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>UIKit has limited support for the soft hyphen. This is the result for setting the string above to a <code>UILabel</code>, <code>UITextView</code> and <code>UIWebView</code> respectively.<br />
<img src="http://blog.tupil.com/wp-content/uploads/2010/06/hyphenate-example.png" alt="" title="Hyphenate example" width="328" height="488" class="alignnone size-full wp-image-131" /></p>
<p>As you can see, <code>UILabel</code> will simply display all soft hyphens.  The behavior of <code>UITextView</code> and <code>UIWebView</code> is more useful: the soft hyphen is shown only when needed and it allows word wrapping.</p>
<h2>Using (X)HTML</h2>
<p>Since <code>UITextView</code> is pretty limited in how much you can style and typeset text, a <code>UIWebView</code> will usually be the way to go for displaying nicely looking, hyphenated text.</p>
<p>Obviously running <code>-stringByHyphenatingWithLocale:</code> on an HTML document will not give the required result. Unfortunately, unless you are willing to use <a href="http://xmlsoft.org/">libxml2</a> directly, your options for working with XML documents on the iPhone are limited.</p>
<p>The best option (as far as I know) is to use <a href="http://code.google.com/p/touchcode/wiki/TouchXML">TouchXML</a>, a friendly wrapper for libxml2 with an API that mimics Cocoa&#8217;s NSXML* classes. However, TouchXML only supports reading XML documents, not creating them. To apply hyphenation, we would need at least a way to modify text nodes. Luckily that turned out to only require a small change to TouchXML, which you can find as a <a href="http://github.com/tupil/hyphenate/blob/master/extras/TouchXML-setStringValue.patch">patch in the hyphenate repository</a>.</p>
<p>Next, after patching and setting up TouchXML, we use a simple XPath expression to fetch all the text nodes and modify each.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    CXMLDocument<span style="color: #002200;">*</span> document <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CXMLDocument alloc<span style="color: #002200;">&#93;</span> init...<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> textNodes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>document nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;//body//text()&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>CXMLNode<span style="color: #002200;">*</span> node <span style="color: #a61390;">in</span> textNodes<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
       <span style="color: #002200;">&#91;</span>node setStringValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>node stringValue<span style="color: #002200;">&#93;</span> stringByHyphenatingWithLocale<span style="color: #002200;">:</span>en<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> hyphenatedDocument <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> 
                                     initWithData<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>document XMLData<span style="color: #002200;">&#93;</span> 
                                     encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Note that this code is a bit of a oversimplification. Whether this simple XPath expression is appropriate for you wholly depends on your actual documents.</p>
<p>Compare the results with and without hyphenation:<br />
<img src="http://blog.tupil.com/wp-content/uploads/2010/06/hyphenate_xml_example_before_after.png" alt="" title="Hyphenate XML Example (before/after)" width="600" height="446" class="alignnone size-full wp-image-140" /></p>
<p>For more information and documentation, check out the <a href='http://github.com/tupil/hyphenate/'>hyphenate repository</a> on GitHub.</p>
<p>(Texts from <a href="http://www.gutenberg.org/etext/1400">Charles Dicken&#8217;s Great Expectations</a>.)</p>
<p><strong>Update 1</strong>: I have now found <a href='http://code.google.com/p/kissxml/'>KissXML</a> to be a better option than TouchXML for my purposes.  Also, it supports setting the text nodes out of the box, no patching necessary!</p>
<p><strong>Update 2</strong>: Frank Zheng has figured out a simple solution to use hyphenation in Core Text. See <a href='http://frankzblog.appspot.com/?p=1'>his blogpost</a> for more information.  Thanks, Frank!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tupil.com/adding-hyphenation-to-nsstring/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>MoProPro: a single command to add testers to iPhone provisioning profiles</title>
		<link>http://blog.tupil.com/mopropro-a-single-command-to-add-testers-to-iphone-provisioning-profiles/</link>
		<comments>http://blog.tupil.com/mopropro-a-single-command-to-add-testers-to-iphone-provisioning-profiles/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 19:38:33 +0000</pubDate>
		<dc:creator>Eelco Lempsink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.tupil.com/?p=96</guid>
		<description><![CDATA[Distributing iPhone applications to testers requires the developer to sign their code using a combination of a (ad hoc) distribution certificate and a mobile provisioning profile, which explicitly contains the device ids (UDIDs) of all your testers. Introducing MoProPro MoProPro automates as much as possible of a very specific, but for iPhone developers very common [...]]]></description>
			<content:encoded><![CDATA[<p>Distributing iPhone applications to testers requires the developer to sign their code using a combination of a (ad hoc) distribution certificate and a mobile provisioning profile, which explicitly contains the device ids (UDIDs) of all your testers.</p>
<h2>Introducing MoProPro</h2>
<p><a href="http://github.com/tupil/MoProPro">MoProPro</a> automates as much as possible of a very specific, but for iPhone developers very common and tedious, task: adding tester&#8217;s UDIDs to an existing ad hoc distribution provisioning profile and downloading the new version of the profile.  While this doesn&#8217;t sound that hard, compare &#8216;Before&#8217; and &#8216;After&#8217;</p>
<p>Both the &#8216;Before&#8217; and &#8216;After&#8217; scenario assume you&#8217;ve already set up your app, a distribution certificate, a mobile provisioning profile and acquired the UDID &#8217;1234567890DEADBEEFCOFFEE098766654321ABCD&#8217; that you want to add to your testers&#8230;</p>
<h2>Before</h2>
<ol>
<li>Go to the iPhone Dev Center website</li>
<li>Click login</li>
<li>Fill in credentials and login</li>
<li>Go to the iPhone Developer Program Portal</li>
<li>Go to the Devices page</li>
<li>Click on Add Devices</li>
<li>Enter and name the UDID, submit</li>
<li>Go to the Provisioning page</li>
<li>Click on Distribution</li>
<li>Find the right (ad hoc) provisioning profile for your app</li>
<li>Click on Edit, click on Modify</li>
<li>Check all boxes of the newly added devices, submit</li>
<li>Wait a bit, refresh the page, until the Download button appears again</li>
<li>Download the new provisioning profile</li>
</ol>
<p>Most of this process is pretty mechanical and boring.  Also, most of this process could be smoother if Apple had made a better web interface, based on common use-cases, which will hopefully happen somewhere in the future.  But, in the meantime, and for people that just like the command prompt, witness &#8216;After&#8217;:</p>
<h2>After</h2>
<p>Using the Terminal, go your project&#8217;s directory (containing the <code>Info.plist</code> file, and a <code>.mopropro</code> file containing the ADC credentials for this project) and run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mopropro 1234567890DEADBEEFCOFFEE098766654321ABCD <span style="color: #ff0000;">&quot;Name for Device&quot;</span>
Saved new provisioning profile to <span style="color: #ff0000;">'Example_Ad_Hoc.mobileprovision'</span>
$</pre></div></div>

<p>That&#8217;s it.</p>
<p>If you add the <code>-v</code> switch, you can see (more or less) what it does, for example:</p>
<pre>
Validating input... Ok.
Getting App Id... com.example.app
Logging in... Ok.
Looking up existing devices... Done.
Adding 1 device... Submitted.
Looking up provisioning profile...
 - Found 5 profiles
 - Found 2 matching profiles
 - Found Ad Hoc profile
 - Adding devices
Saving profile with new devices... Ok.
Waiting a bit while the new profile is generated... Ok.
Trying to retrieve new profile... Got it!
Saved new provisioning profile to 'Example_Ad_Hoc.mobileprovision'
</pre>
<p>The script has some more options and can also accept multiple devices.  All &#8216;documentation&#8217; can be found in the <a href="http://github.com/tupil/MoProPro/blob/6ba12ecaa9ad21b6386d4f5a26ee8a58342ca555/README.md">README</a> in <a href="http://github.com/tupil/MoProPro/tree/master">MoProPro&#8217;s repository on GitHub</a>. Enjoy!</p>
<h2>About</h2>
<p>MoProPro uses the excellent <a href="http://mechanize.rubyforge.org/mechanize/">WWW::Mechanize</a> library to automate using the iPhone Dev Center website. The <a href="http://plist.rubyforge.org/">Plist</a> library is used for getting the application identifier out of the <code>Info.plist</code> file.  <a href="http://www.yaml.org/">YAML</a> is used as the format for the configuration and input files.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tupil.com/mopropro-a-single-command-to-add-testers-to-iphone-provisioning-profiles/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
