Chapter 10. Text

Drawing text into your app’s interface is one of the most complex and powerful things that iOS does for you. But iOS also shields you from much of that complexity; all you need is some text to draw, and possibly an interface object to draw it for you.

Text to appear in your app’s interface will be an NSString (bridged from Swift String) or an NSAttributedString. NSAttributedString adds text styling to an NSString, including runs of different character styles, along with paragraph-level features such as alignment, line spacing, and margins.

To make your NSString or NSAttributedString appear in the interface, you can hand it to an interface object that knows how to draw it, or you can draw it into a graphics context yourself:

Text-drawing interface objects

Interface objects that know how to draw an NSString or NSAttributedString are:

UILabel

Displays text, possibly consisting of multiple lines; neither scrollable nor editable.

UITextField

Displays a single line of editable text.

UITextView

Displays multiline text; can be scrollable and editable.

Self-drawing text

Both NSString and NSAttributedString have methods for drawing themselves into any graphics context.

Deep under the hood, all text drawing is performed through a low-level technology with a C API called Core Text. At a higher level, iOS provides Text Kit, a middle-level technology lying on top of Core Text. UITextView is largely just a lightweight wrapper around Text Kit, and Text Kit can also ...

Get Programming iOS 13 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.