Chapter 11. Rich-Text Handling

In Chapter 10, we showed how Cocoa’s multiple-document architecture takes care of many of the tasks involved in building a document-centric application, and we used a simple text editor as an example. Cocoa’s text-handling ability goes much further by supporting multiple fonts, various paragraph styles, embedded images, undo, drag-and-drop, and even spell checking. It can handle differences in text directionality and provides sophisticated typesetting capabilities, such as the ability to control kerning between characters.

In this chapter, we are going to examine the functionality of NSTextView and the other classes that compose the text system. We’ll then dive into code and add the following functionality, one step at a time, to a rich-text editor:

  • Enable the font menu

  • Work with attributed text

  • Register undo actions

  • Enable the text menu

  • Handle embedded images

  • Add a special feature that we’ll save for last

That’s a lot of ground to cover, so let’s get going!

Cocoa’s Text System

Cocoa’s text system, which underlies the functionality we worked with in Chapter 10, consists of three API layers, as shown in Figure 11-1. You can see the same Model-View-Controller (MVC) paradigm we’ve talked about previously in the design of the text system. At the top layer is the NSTextView that provides the on-screen view of text.

Three layers of the Cocoa text system
Figure 11-1. Three layers of the Cocoa text ...

Get Learning Cocoa with Objective-C, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.