What is Text Kit?
Apple sums it up best in their Text Programming Guide for iOS:
“Text Kit is a set of classes and protocols in the `UIKit` framework that provide high-quality typographical services which enable applications to store, lay out, and display text with all the characteristics of fine typesetting, such as kerning, ligatures, line-breaking, and justification.”
In the past, if you wanted to do advanced text layout, such as labels with different styles, or images and text in the same text view, you would generally fall back on a
UIWebView or would have to dig deep into Core Text. In iOS 6,
UITextView were given an
NSAttributedString property that improved upon some of these obstacles but remained difficult to implement and still lacked some important features. Text Kit changes all of this.
Text Kit is a fast, modern, text layout and rendering engine built on Core Text that has great integration with
UILabel have been rebuilt on Text Kit and all include support for paginated text, text wrapping, rich text editing, interactive text coloring, text folding and custom truncation. Each of these UI elements are now structured the same way. In the back, an
NSTextStorage object contains the guts of your text. This is a subclass of
NSMutableAttributedString, which allows for batched edits. This means you can change the style of characters in range without having to replace the entire body of the text.
[self.textView.textStorage beginEditing]; [self markWord:@”Alice” inTextStorage:self.textView]; [self.textView.textStorage endEditing];
The text storage manages a set of
NSLayoutManagers, notifying them of any changes to its characters or attributes so that they can relay and redisplay the text as needed. Above the layout manager is an
NSTextContainer which defines the coordinate system and geometry for the layout manager. For instance, if you want the text in a
UITextView to flow around an image, you would define an exclusion path on the text container.
UIBezierPath *exclusion = ButterflyBezierPath; self.textView.textContainer.exclusionPaths = @[exclusion];
The text container can handle hit tests, making it possible to map a tap to characters in the text, and also provides new delegate methods to allow developers to define how Data Detector links should be handled.
UITextView on top of Text Kit, Apple has given developers a lot more flexibility and power to design rich text views. The restructuring also simplifies using these elements as they are all designed the same way, and each has all the benefits of sitting on top of Core Text. Generally, with more power and flexibility comes more setup and management. But, if all you need is a simple label with some text, the simple setups will still work as they always have.
self.textLabel.text = @”Hello Text Kit”;
This is the third in an 11-part Developer’s Guide to iOS 7. You can find the full guide here. For more information on how Double Encore can help you prepare your company for the changes in iOS 7, please email us.