Now that we have defined the RTF class, let’s integrate it into the MathPaper application to get the promised fonts and formatting:
Open MathPaper.pbproj
in Project Builder and
choose File → New File to create a new file called
RTF.h
.
Select Empty File in the New File Assistant pane and then click Next.
Enter “RTF.h” in the File Name field, make sure the MathPaper target is checked, and then click Finish.
Now enter the RTF class interface code, as provided in the previous
section, into RTF.h
.
Similarly, create another file called RTF.m
and
insert the RTF class implementation code (also provided earlier) into
it.
If they aren’t there already, drag the
RTF.h
and RTF.m
files into
the Classes group in PB’s Groups & Files pane.
Another way to perform the six steps presented in this section is to
first create the RTF.h
and
RTF.m
class files outside of PB, using another
editor (e.g., TextEdit or GNU Emacs). Save these two files in the
~/MathPaper
folder, and then drag their file
icons from the Finder and drop them into the Classes group in
PB’s Groups & Files pane.
Now that our new RTF class has been added to the project, we proceed to make the necessary changes in the PaperController class.
Insert the following directive after the other
#import
statement near the top of
PaperController.m
:
#import "RTF.h"
Insert the new appendRTFData: method declaration shown here into the existing NSTextView(MathPaper) category interface:
@interface NSTextView(MathPaper)
- (void)appendString:(NSString *)str;- (void)appendRTFData:(NSData *)str;
@end
Insert the new appendRTFData: method implementation shown here into the NSTextView(MathPaper) category implementation:
- (void)appendRTFData:(NSData *)data { int len = [ [self string] length]; [self replaceCharactersInRange:NSMakeRange(len,0) withRTF:data]; }
Replace the gotData: method
implementation in PaperController.m
with the new
implementation shown here:
#define USE_RTF - (void)gotData:(NSNotification *)not { NSData *data; NSString *str; #ifdef USE_RTF RTF *rtf = [[[RTF alloc] init] autorelease]; #endif data = [ [not userInfo] objectForKey:NSFileHandleNotificationDataItem]; str = [ [NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; [str autorelease]; /* Automatically release when done */ // Add the data to the end of the text object#ifdef USE_RTF [rtf setBold:YES]; [rtf setJustify:NSRightTextAlignment]; [rtf setSize:20]; [rtf appendString:str]; [rtf setBold:NO]; [rtf setJustify:NSLeftTextAlignment]; [rtf setSize:12]; [rtf appendString:@"-------------------- "]; [theText appendRTFData:[rtf data]]; #else [theText appendString:str]; [theText appendString:@"-------------------- "]; #endif // Now scroll to the bottom [theText scrollRangeToVisible: NSMakeRange([[theText textStorage] length], 0)]; // Register to get the notification again [fromEvaluator readInBackgroundAndNotify]; // Finally, allow the user to make any changes to the text [theText setEditable:YES]; }
Notice that we
used the USE_RTF
conditional compilation technique
so that RTF can be turned off when desired without doing a lot of
editing. This is a useful technique when you are developing code.
Second, notice that we alloc,
init, and autorelease the RTF string all in one place.
This assures that the string will be properly freed when we are done
using it; we don’t have to bother with a separate
release message.
Save all pertinent files, and build and run your MathPaper project. The RTF object should behave as shown in the screen shot on the left in the earlier Figure 12-1.
3.138.37.151