Yes, Virginia, There is Such a Thing as HTML-formatted Text in tvOS

There’s been a lot of hand-wringing about the lack of web view functionality on the new 4th generation Apple TV. If your company is planning a tvOS app, you may be concerned that there is no way to display HTML on the new Apple TV because it doesn’t have a web browser. Even though many iOS apps don’t display a browser window per se, they do use a “web view” to show static but highly formatted HTML text. It’s just a lot easier than creating “attributed text” by hand or a complex series of labels in your app.

Well, fear not, because this is very possible on tvOS.

Apple TV screenshot of Greann Gaeilge app, showing HTML-formatted text.

Apple TV screenshot of Greann Gaeilge app, showing HTML-formatted text.

I’ll explain how my app Greann Gaeilge did just that.

Apple TV’s tvOS is an exciting new platform that many businesses want to be on, and the good news is that it’s mostly a subset of iOS in terms of APIs. So it’s actually pretty straightforward to move an iOS app to tvOS.

One of the problems, though, is that that subset does not include WebKit, so there’s no obvious way to display complex text using simple HTML attributes. But, as usual, Apple has been planning for this potential for years.

Back in iOS 7, Apple added “NSHTMLTextDocumentType” as an option when creating NSAttributedString. Here’s a Stack Overflow question with the solution–but don’t pay attention to the accepted answer (marked with a green checkbox) because it’s outdated. The real solution is the answer upvoted 180 times (at the time of this writing) written by member Pix.

If you read the comments here and elsewhere on the web, you might think performance is a huge problem. But it appears that, with iOS 9, Apple no longer instantiates a web view in the background–it’s handling the HTML parsing much faster, presumably natively. I have not experienced any performance problems on tvOS.

But wait–there’s more! You might be thinking that HTML text won’t respond to Apple’s Dynamic Type options. Yes, you can even use Dynamic Type font styles such as -apple-system-body and -apple-system-headline. See this What’s New in Safari document, specifically the section on Font Enhancements and Dynamic Type, for the font tags you can use for Dynamic Type.

I want to be clear that you can’t use this to display a full web view with video and links. It’s primarily a way of displaying formatted text (and, from what I’ve read, images). But it’s a nice surprise for many businesses that this functionality exists.

So get crackin’ on planning that tvOS version of your iOS app. Now you just need to focus on… focus. But I’ll discuss that in a future post.

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s