Need to Print a Custom Header using obj-c

464 Views Asked by At

I am creating an NSView and it prints fine with this piece of code :

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setHorizontallyCentered:YES];
[printInfo setVerticallyCentered:YES];
NSPrintOperation *operation = [NSPrintOperation printOperationWithView:printView printInfo:printInfo];

NSPrintPanel *printerPanel = operation.printPanel;

printerPanel.options = NSPrintPanelShowsPaperSize | NSPrintPanelShowsPageRange | NSPrintPanelShowsOrientation | NSPrintPanelShowsPageSetupAccessory | NSPrintPanelShowsPreview;

[operation runOperationModalForWindow:window delegate:nil
                       didRunSelector:nil contextInfo:nil];

I also have this code in applicationDidFinishLaunching

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@YES forKey:NSPrintHeaderAndFooter];

Now if i try to override these methods

 - (void)drawPageBorderWithSize:(NSSize)pageSize
 - (NSAttributedString *)pageHeader

They are not even getting called. Anybody know why?

2

There are 2 best solutions below

0
On BEST ANSWER

In order for you to over-ride a NSView's methods you first have to sub-class the file and then place your overridden methods inside of the new custom class. Instantiate and use your subclass instead of NSView and remember to make the super calls in the overridden methods so everything works as expected. Shouldn't be too much else to it, if you've already got a sub-class you can post it and I can take a look.

0
On

Actually i did this : for a multi-line-label i have in my .xib i added the custom class in the inspector

then i have this .h & .m files - it works for text but not for images

NSTextFieldForPrint.h :

#import <Cocoa/Cocoa.h>
@interface NSTextFieldForPrint : NSTextField
@end

NSTextFieldForPrint.m

#import "NSTextFieldForPrint.h"
@implementation NSTextFieldForPrint
- (NSAttributedString *)pageHeader
{
    NSLog(@"Am i called?");
    NSAttributedString *theHeader = nil;
    NSString *myImagePath = [[[NSBundle mainBundle] resourcePath]  stringByAppendingString:@"header.jpg"];
    NSImage *pic = [[NSImage alloc] initWithContentsOfFile:myImagePath];
    NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    [attachment setAttachmentCell: attachmentCell ];
    theHeader = [NSAttributedString  attributedStringWithAttachment: attachment];
    return theHeader;

    //return [[NSAttributedString alloc] initWithString:@"adsfasdf"];
}

@end