You know what’s better than UITextView?
At least, if all you need to do is display stuff. If you need editing, of course UITextView is what you want. But I’m working on something that only needs to display text. I started out using UITextView because well… I need to display text, it’s only logical to use the text view (duh!). And while there’s some cool stuff in there, like data-detecting URL’s and automagically turning them into tappable hyperlinks that open up in Safari, it’s just not the same. Oh sure, you can read in an RTF file to have all sorts of spiffy formatting and layout, but I’m pulling my text out of an SQLite database. Lacking good SQLite editing tools or whipping up my own editor tool, plus wanting a way to have rich media depictions, I realized: HTML! I can have text, I can format it however I want, I can easily edit it in the lame SQLite tools available, I can have images, I can have tappable links, I could even put in movies, references from elsewhere online…. so many possibilities!
So really, if you just need to display text to an iOS user but want something richer than plain old text, UIWebView is so simple to use.
You should do a few things to ensure a good experience.
First, set your UIViewController (typically, but use whatever is appropriate) to be the UIWebView’s delegate (and heed UIWebView’s documentation about its delegate property). Then have the delegate object respond accordingly:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
// If there's anything clickable, we don't want to show it inline; open up Safari to show it.
if (webView == self.myWebView)
if (navigationType == UIWebViewNavigationTypeLinkClicked)
[[UIApplication sharedApplication] openURL:request.URL];
(Apologies for the crappy code formatting… wordpress.com’s editor seems to be doing funny things to code and I can’t get it to play nice). What this does is “redirect” any clicks upon links. As it is, if a user tapped on a link, that link would open up right there in the UIWebView, which may not be the desired user experience. So, using the above code you can redirect any link taps to an external web browser (Safari), and maintain your UIWebView as a pretty panel of stuff. Nice.
I’m in process of converting my UITextView’s to UIWebView’s. It’s a hassle, but it’s generating better results. I can keep all of my data as simple text streams, I can format it and have a high degree of control over the formatting, I can include rich media. What’s not to like?