Chapter 12. Rich Text Format and NSText

The MathPaper windows that we built in the last chapter didn’t quite live up to their initial billing. We promised what you see on the left side of Figure 12-1 but gave what you see on the right. The difference between the two windows is a matter of fonts and formatting. Although the NSTextView object that we used allows a great deal of control over fonts and formatting, when we invoked the replaceCharactersInRange:withString: method in PaperController we were simply pasting plain ASCII text into the selection, which comes up as left-justified monofont text — not very interesting. To get the promised fonts and formatting, we’ll have to learn about Rich Text Format (RTF). Most of today’s word processors (e.g., TextEdit, Microsoft Word) support RTF, and it has been used as a cross-platform format for years, so you probably know a little bit about it already. In this chapter, we’ll show you how to code RTF right into your applications.

What we promised (left) and what we’ve delivered so far (right)

Figure 12-1. What we promised (left) and what we’ve delivered so far (right)

Rich Text Format

Suppose you want to amaze your friends by showing them how easy it is to create text with different font sizes in a window. You might want the final window to look something like that shown in Figure 12-2.

Desired text output

Figure 12-2. Desired ...

Get Building Cocoa Applications: A Step by Step Guide 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.