Cocoa Programming for Mac OS X - Aaron Hillegass [90]
You have already created a view that knows how to generate PDF data to describe how it is supposed to look. Getting the PDF data into a file is really quite easy. NSView has the following method:
- (NSData *)dataWithPDFInsideRect:(NSRect)aRect
This method creates a data object and then calls drawRect:. The drawing commands that would usually go to the screen instead go into the data object. Once you have this data object, you simply save it to a file.
Open BigLetterView.m and add a method that will create a Save panel as a sheet. We’ll use a block again, as we did with NSOpenPanel in Chapter 18, to respond to the user’s actions.
- (IBAction)savePDF:(id)sender
{
__block NSSavePanel *panel = [NSSavePanel savePanel];
[panel setAllowedFileTypes:[NSArray arrayWithObject:@"pdf"]];
[panel beginSheetModalForWindow:[self window]
completionHandler:^ (NSInteger result) {
if (result == NSOKButton)
{
NSRect r = [self bounds];
NSData *data = [self dataWithPDFInsideRect:r];
NSError *error;
BOOL successful = [data writeToURL:[panel URL]
options:0
error:&error];
if (!successful) {
NSAlert *a = [NSAlert alertWithError:error];
[a runModal];
}
}
panel = nil; // avoid strong ref cycle
}];
}
Also, declare the action method in the BigLetterView.h file:
- (IBAction)savePDF:(id)sender;
Open MainMenu.xib. Select the Save As... item under the File menu. If there is no a Save As item, drag a new Menu Item from the Library onto the File menu. Relabel it Save As PDF.... (You may delete all the other menu items from the menu, if you wish.) Make the Save As PDF... menu item trigger the BigLetterView’s savePDF: method (Figure 20.3).
Figure 20.3. Connect Menu Item
Save and build the application. You should be able to generate a PDF file and view it in Preview (Figure 20.4).
Figure 20.4. Completed Application
You will notice that multikeystroke characters (such as é) are not handled by your BigLetterView. To make this possible, you would need to add several methods that the NSInputManager uses. This topic is beyond the scope of this book (we just wanted to show you how to get keyboard events), but you can learn about it in Apple’s discussion of NSInputManager in the documentation.
For the More Curious: NSFontManager
Sometimes, you will have a font that is good but would be perfect if it were bold or italicized or condensed. NSFontManager can be used to make this sort of conversion. You can also use a font manager to change the size of the font.
For example, imagine that you have a font and would like a similar font but bold. Here is the code:
fontManager = [NSFontManager sharedFontManager];
boldFont = [fontManager convertFont:aFont toHaveTrait:NSBoldFontMask];
Challenge 1
Give the letter a shadow. The NSShadow class has the following methods:
- (id)init;
- (void)setShadowOffset:(NSSize)offset;
- (void)setShadowBlurRadius:(float)val;
- (void)setShadowColor:(NSColor *)color;
Challenge 2
Add the Boolean variables bold and italic to your BigLetterView. Add check boxes that toggle these variables. If bold is YES, make the letter appear in boldface; if italic is YES, make the letter appear in italics.
Chapter 21. Pasteboards and Nil-Targeted Actions
A process called the pasteboard server (/usr/bin/pboard) runs on your Mac. Applications use the NSPasteboard class to write data into that process and to read data from that process. The pasteboard server makes possible such operations as copying, cutting, and pasting between applications.
An application can copy the same data onto the pasteboard in several formats. For example, an image can be copied onto the pasteboard as a PDF document and as a PNG image. Then the application that reads the data can choose the format that it likes most. The pasteboard uses UTIs to identify the