Chapter 12. Printing

In the past, code to handle printing has been difficult to write. Many factors, such as margins, page orientation, and paper size, come into play. The differences in drawing models (between screen drawing and drawing to the printer) have made things even more challenging. Cocoa provides a clean printing interface that uses the same drawing model used to draw to the screen. In addition, just as Cocoa supports the use of calibrated color when drawing to the screen, ColorSync provides advanced color management when printing to paper.

As we saw in Chapter 7, when a view object receives a drawRect: message, it responds by drawing itself into the current graphics context. Usually, this context is a frame buffer, and the drawRect: message results in the view drawing its contents to the screen. The same machinery is used during printing, but the current graphics context is set to be the printer.

Remember (as discussed in Chapter 7) that the drawRect: method is never called directly to make a view appear on the screen; it is called only when the display message is sent to the view. The same methodology applies to printing; the drawRect: method is called when a print: message is sent to the view. Calling print: on a view causes the AppKit to display a print panel that asks the user which printer she wishes to use (along with page setup information). When the user clicks the Print button of the print panel, a sequence of drawRect: messages—one for each page—is sent ...

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.