我正在创建一个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他们甚至都没接到电话。有人知道为什么吗?
发布于 2015-05-06 02:42:42
为了超越NSView的方法,首先必须对文件进行子类处理,然后将重写的方法放在新的自定义类中。实例化并使用您的子类而不是NSView,并记住在重写的方法中进行超级调用,这样一切都能按预期工作。不应该是太多的其他,如果你已经有一个子类,你可以张贴,我可以看看。
发布于 2015-05-08 01:40:17
实际上,我这样做了:对于.xib中的多行标签,我在检查器中添加了自定义类。
然后我有了这个.h & .m文件--它适用于文本,但不适用于图像
NSTextFieldForPrint.h:
#import <Cocoa/Cocoa.h>
@interface NSTextFieldForPrint : NSTextField
@endNSTextFieldForPrint.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"];
}
@endhttps://stackoverflow.com/questions/30066252
复制相似问题