需要使用obj-c打印自定义标头

时间:2015-05-06 02:06:26

标签: objective-c header nsprintoperation nsprintinfo

我正在创建一个NSView,它使用这段代码打印得很好:

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];

我在applicationDidFinishLaunching

中也有这段代码
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@YES forKey:NSPrintHeaderAndFooter];

现在,如果我试图覆盖这些方法

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

他们甚至没有被召唤。有谁知道为什么?

2 个答案:

答案 0 :(得分:0)

为了覆盖NSView的方法,首先必须对文件进行子类化,然后将重写的方法放在新的自定义类中。实例化并使用您的子类而不是NSView,并记住在重写的方法中进行超级调用,以便一切按预期工作。如果你已经有一个子类,你可以发布它,我可以看一看,不应该是它太多了。

答案 1 :(得分:0)

其实我这样做了:对于我在.xib中的多行标签,我在检查器中添加了自定义类

然后我有了这个.h& .m文件 - 它适用于文本但不适用于图像

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