Creating an RTF Class

Using Rich Text can be a pain: you have to remember far too many controls. There’s got to be an easier way! Of course there is — create an Objective-C class for building and managing Rich Text segments. Following is the interface for such a class, a subclass of the root NSObject class, which works with the NSTextView object:

#import <Cocoa/Cocoa.h>

@interface RTF:NSObject
{
    NSMutableData *data;
}
- (void)dealloc;
- (NSData *)data;
- (void)appendChar:(unsigned char)ch;
- (void)appendRTF:(NSString *)string;
- (void)appendString:(NSString *)string;
- (void)setBold:(BOOL)flag;
- (void)setJustify:(NSTextAlignment)mode;
- (void)setSize:(float)aSize;

@end

This time we’ll describe these methods before we show how they are coded. The interface and method descriptions are all you really need to know in order to use a class — we don’t need to know the implementation details. For example, we don’t know how the AppKit classes are implemented, yet we’ve been using them throughout the book!

Table 12-3 summarizes the instance methods declared in our new RTF class. The methods described in this table that do not appear in RTF.h are overrides of methods in NSObject.

Table 12-3. Instance methods in the RTF class

Method

Method description

(id)init

Initializes an RTF object, establishes a simple Rich Text header, and returns the id of the newly created RTF object.

(void)dealloc

Frees the RTF object and releases its internal storage.

(NSData *)data

Returns an NSData ...

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.