I have the following code as my controller to try to print a view. The print job dialog displays, but nothing prints:
-- Kaydell
Code:
//
// PrintTestAppDelegate.m
// PrintTest
//
// Created by kaydell on 4/27/10.
// Copyright 2010 LevSolve.com. All rights reserved.
//
#import "PrintTestAppDelegate.h"
@implementation PrintTestAppDelegate
@synthesize window;
@synthesize printTestView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (IBAction) print: (id) sender {
NSLog(@"print:");
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:printTestView printInfo:printInfo];
[printOp runOperation]; // <<<<<<<<<<< this displays a print job dialog <<<<<<<<<<<<<<<
}
@end
//
// PrintTestView.m
// PrintTest
//
// Created by kaydell on 4/27/10.
// Copyright 2010 LevSolve.com. All rights reserved.
//
#import "PrintTestView.h"
@implementation PrintTestView
- (void)drawString:(NSString *)string centeredInRect:(NSRect)r withAttributes:(NSDictionary *)theAttribute
{
NSPoint stringOrigin;
NSSize stringSize;
stringSize = [string sizeWithAttributes:theAttribute];
stringOrigin.x = r.origin.x + (r.size.width - stringSize.width)/2;
stringOrigin.y = r.origin.y + (r.size.height - stringSize.height)/2;
[string drawAtPoint:stringOrigin withAttributes:theAttribute];
}
- (void) drawHeaderRect: (NSRect) dirtyRect {
NSRect bounds = [self bounds];
[self drawString:@"Header" centeredInRect:bounds withAttributes:nil];
}
- (void) drawBodyRect: (NSRect) rect {
NSRect bounds = [self bounds];
[self drawString:@"Body" centeredInRect:bounds withAttributes:nil];
}
- (void) drawFooterRect: (NSRect) rect {
NSRect bounds = [self bounds];
[self drawString:@"Footer" centeredInRect:bounds withAttributes:nil];
}
- (void) drawRect: (NSRect) rect { <<<<<<<<< this is never called <<<<<<<<<<<<<<<<<<<
if (![NSGraphicsContext currentContextDrawingToScreen]) {
[self drawHeaderRect:rect];
[self drawBodyRect:rect];
[self drawFooterRect:rect];
}
}
@end
-- Kaydell