首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要使用obj-c打印自定义头

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

Stack Overflow用户
提问于 2015-05-06 02:06:25
回答 2查看 426关注 0票数 1

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

代码语言:javascript
复制
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中也有这段代码

代码语言:javascript
复制
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@YES forKey:NSPrintHeaderAndFooter];

如果我试图覆盖这些方法

代码语言:javascript
复制
 - (void)drawPageBorderWithSize:(NSSize)pageSize
 - (NSAttributedString *)pageHeader

他们甚至都没接到电话。有人知道为什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-06 02:42:42

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

票数 0
EN

Stack Overflow用户

发布于 2015-05-08 01:40:17

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

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

NSTextFieldForPrint.h:

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>
@interface NSTextFieldForPrint : NSTextField
@end

NSTextFieldForPrint.m

代码语言:javascript
复制
#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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30066252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档