As I've been struggling to get URLs underlined and tappable in a Core Text UIView, I'd like to share my findings here.
Drawing text using Core Text is done by following these steps:
Implementing these steps results in this code:
Now, if you want to support tappable URLs, you need to do some more advanced stuff:
The implementation might look like this:
As can be seen, this is a bit more complex. Using the information we stored in [4], we can implement touch handling in our UIView. Each time a touch is detected, we should check it's location, map it to a bounding box of a single line and get the range within the attributed string. Given that range, we should loop over each character in the attributed string to check whether any character contains the custom attribute we set during [1]. If so, we fetch the value for the custom attribute (which holds the URL, see [1]) and launch the URL.
To make things even more complex, you could add hyphenation support to your attributed string. Core Text luckily honors UTF-8 soft-hyphen characters (0x00AD) in order to split strings but doesn't show a hard hyphenation character (-). In order to get this working properly, you can check out Tupil's Blog. Unfortunately the hyphenation library suggested by Tupil also inserts soft hyphens for URLs which then breaks Apple's NSDataDetector class we use. So I've made small change to NSString+Hyphenate.m, in order to not hyphenate URLs (note this change is not fool-proof but does the trick for my purposes). Within stringByHyphenatingWithLocale in the loop which iterates over all tokens, I added this additional check:
The resulting code is now exactly what I wanted. Core Text in combination with hyphenation support and tappable URLs.