Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display formatted text on the iPhone

I need to display text in an iPhone application, something like the way a book is displayed, for example:

Heading

Sub heading

The actual text of the book. Blah. Blah. Blah.


How would I go about doing that? I've found the UITextView and UITextField and UIScrollView objects, but I can't figure out how to use them properly... Any suggestions?

I hope that makes sense...

like image 708
Miquella Avatar asked Feb 03 '26 23:02

Miquella


2 Answers

You could use HTML in a UIWebView. Or layout a view with multiple UILabels set for particular fonts/sizes/properties. Then use a UITextField for the rest of the unformatted text. In desktop cocoa you can use attributed strings, but I don't think those are available for the iphone.

like image 111
Ryan Townshend Avatar answered Feb 05 '26 13:02

Ryan Townshend


As Ryan said, you can use HTML with a WebView, but if you want to stick to native text drawing, you're going to have to drop down to CoreGraphics and draw all the text by hand. This is a lot of work, but if done right it will be more efficient and have a lower memory footprint than using WebView.

Edit: just took a look at your requirements again, and if only the heading and subheading require style changes, then I would recommend just using separate UILabels for those. You can also call -sizeToFit on the UILabels after assigning their text/font properties so they'll fit their text, which will let you handle wrapped headers/subheaders.

like image 34
Lily Ballard Avatar answered Feb 05 '26 11:02

Lily Ballard