BuySellAds.com

My book Barcodes with iOS is nearing completion. Buy it now to get early access!
Our DNA is written in Objective-C
Jump

Category Archive for ‘Recipes’ rss

Visual View Debugging

In this article I want to summarize a couple of “barefoot techniques” for tuning your views. Sometimes you are painstakingly putting together a UI with multiple views and you cannot tell any more where one ends and another begins.

The debugger is not really working well on this because most of the interesting information about views is hidden behind properties and you cannot usefully inspect their current contents because properties are really methods the value of which is only set when you actually call them.

But I want to share some easy techniques that I started using so that I can get this visual puzzle untangled.

Read more

Splitting a String into Paragraphs … with Blocks

I found myself in need of splitting an NSString into paragraphs. Or more precisely to analyze a string and find the NSRange for each such paragraph. At first I wrote a C-style function that looked for the ‘\n’ in the NSString’s utf8String, but it turns out that this approach has problems with multi-character UTF8 sequences.

For the longest time I shirked from using Blocks, which became part of iOS with iteration 4.0. But since this project will have a minimum deployment target of higher than this, I gained the ability to use a block-based enumeration function to achieve my goal with a record minimum of code.

Read more

Cloning a Git Repo with Submodules

I was setting up a project with one submodule on my working iMac and was wondering how to do this most quickly. After tweeting the approach I had found, there where quickly some very smart people responding on how to do that better. I found this kind of crowd-sourced incremental improvement exhilarating, so I’m sharing it with you.

Read more

Expanding/Collapsing TableView Sections

While giving many designers a headache the Twitter app still serves as template on how to solve a variety of UX riddles. One of which is the situation where one might want to have sections in a tableview that possess the ability to expand from one row to several and collapse vice versa.

The eye of the experienced developer sees two challenges contained therein: 1) grafting a mechanism for collapsing and expanding onto UITableView in a reusable way 2) making custom accessory views that look like a rotated version of the disclosure indicator, pointing upwards or downwards and also changing color when highlighted.

In this article I present my solution to this UX riddle. At the same time I will demonstrate how NSMutableIndexSet can be used to our advantage. In contrast to the pull-to-reload method previously discussed, this does not contain anything remotely patentable.

Update March 12th, 2013: Cleaned up version of the custom-colored accessory is now available via DTFoundation, the example project is now part of our Examples collection on GitHub. Please note that if you use this code you have to attribute it to us or buy a Non-Attribution License.

Read more

Git Submodules and Xcode 4

Previously when integrating existing library code with new projects I would have simply copied the necessary groups from from Xcode project to another. Then I would choose to copy the files to the new project to be sure that they got included in the source tree. Otherwise the project would not build for other people accessing the same source control management (SCM) server.

Now with Xcode 4 this technique no longer works. You simply cannot drag groups between workspaces. The question that interests us today is how we can add an existing GitHub project to our own.

Let’s see if we can figure out a simple and duplicatable method to achieving this. It would make our lives much easier to not having to duplicate component code for each new project.

Read more

HTML Entities

Similar to the previous article on decoding HTML colors we also need to decode HTML entities like ". So I found an authoritative list on the web, courtesy of Wikipedia and in this article I will demonstrate how to use quickly hacked up command line tool to convert it into Objective-C.

This shall serve as an example as to how quickly you can leverage your objC knowledge to build a useful tool for such a one-off operation. If you know how to reuse your skills from iOS development on command line tools then you can always quickly whip up a one-off tool to do some work that otherwise you would have needed to do manually with a text editor.

Read more

Starting an OpenSource Project on GitHub

In my article on CoreText I mentioned that Apple left out some very useful methods from NSAttributedString, namely the ones that would allow you to create an attributed string from HTML. Now you would probably not want to create attributed strings from complex HTML documents, but use that for simple tasks like displaying one word in a different color or bold.

My first thought was that maybe I should make it a sellable component, but I dismissed this idea for two reasons:

  1. there is a certain likelyhood that Apple will implement the missing functions in the next major SDK refresh
  2. if I would get help from people who are more experienced in dealing with HTML parsing then this would benefit everybody involved

So I posted the question on Twitter as to where to put the shared code. The response was a resounding GitHub (one mention of Mercurial and Assembla each). So far I had put off dealing with GitHub, because – in contrast with many other iOS developers – I happen to very much like Subversion integration in Xcode.

Read on to learn how to get started.

Read more

HTML Colors

I am building a category for NSAttributedString that will allow me to make attributed strings from HTML code. For coloring the text there are two methods in HTML, via the deprecated font tag and via style. Both use color names or hex to describe the color.

The W3C knows 147 color names, clearly too many to type in manually. So I used a bit of shell script magic to hammer this into Objective-C. Then I also needed an elegant method to make regular UIColor objects from a hex string.

Both I am sharing in this article. These methods are invaluable if you are dealing with web people who are used to specifying colors this way. Also it might be more intuitive if you can specify colors in hex format yourself. That is, if you are a geek who is used to thinking in the hexadecimal system.

Read more

Befriending Core Text

Before the iPad was released you had basically two ways how to get text on screen. Either you would stick with UILabel or UITextView provided by UIKit or if you felt hard-core you would draw the text yourself on the Quartz level incurring all the headaches induced by having to mentally switch between Objective-C and C API functions.

As of iOS 3.2 we gained a third alternative in Core Text promising full control over styles, thread safety and performance. However for most of my apps I did not want to break 3.x compatibility and so I procrastinated looking at this powerful new API. Apps running only on iPads could have made use of Core Text from day 1, but to me it made more sense supporting iPad via hybrid apps where the iPhone part would still be backwards compatible.

Now as the year has turned the adoption of 4.x on all iOS platforms is ever more accelerating. Many new iPads where found under the Christmas tree and by now even the most stubborn people (read needing 3.x for jailbreaking and sim-unlocking) have little reason to stick with 3.x. Thus we have almost no incentive left to stick with 3.x compatibility. Yay!

Read more

Detecting Taps Outside of Tableview Cells

iWoman Date Picker

In the latest version of iWoman- which I’ve been working on the past few weeks – I had a situation where I am sliding up a date picker when the user taps on a date cell. Sliding it out when the user taps on other cell was easy, you can do that in the other cells’ didSelectRowAtIndexPath. But if you have that in a grouped tableview then there are several areas outside of cells that also could become the targets of a user’s taps.

Generally a user would also assume to be able to dismiss the picker by tapping there, in headers or empty areas where you see the background shine through. In this blog post I’m showing you a technique on how this is done most efficiently and also backwards compatible.

I tried out several approaches before settling on this solution. You might be able to use a gesture recognizer, but that would rule out 3.1 compatibility. You could override the touch methods of the table view and do some fancy detective work there. But I found – as I did so often before – that by far the most convient way is to override hitTest.

Read more