1

我正在创建一个 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

他们甚至没有被叫到。有人知道为什么吗?

4

2 回答 2

0

为了让您覆盖 NSView 的方法,您首先必须对文件进行子类化,然后将覆盖的方法放在新的自定义类中。实例化并使用您的子类而不是 NSView 并记住在覆盖的方法中进行超级调用,以便一切按预期工作。其他的就不多说了,如果你已经有子类可以贴出来我看看。

于 2015-05-06T02:42:42.567 回答
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
于 2015-05-08T01:40:17.753 回答