是否可以更改NSTextView的标题高度?

时间:2012-01-27 18:29:13

标签: objective-c cocoa nstextview

我正在从NSTextview保存PDF并在标题中添加徽标。我覆盖了pageHeader并且徽标出现了,但它被剪裁了。

是否可以更改NSTextView的标题高度?

谢谢!

部分代码:

-(IBAction)impLaudo:(id)sender 
{
    NSPrintInfo *printInfo;
    NSPrintInfo *sharedInfo;
    NSPrintOperation *printOp;
    NSMutableDictionary *printInfoDict;
    NSMutableDictionary *sharedDict;

    sharedInfo = [NSPrintInfo sharedPrintInfo];
    sharedDict = [sharedInfo dictionary];
    printInfoDict = [NSMutableDictionary dictionaryWithDictionary:sharedDict];

    [printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
    [printInfoDict setObject:[[dirLaudos stringByAppendingString:[estudo stringValue]] stringByAppendingString:@".pdf"] forKey:NSPrintSavePath];

    printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
    [printInfo setHorizontalPagination: NSClipPagination];
    [printInfo setVerticalPagination: NSAutoPagination];
    [printInfo setVerticallyCentered:NO];
    [[printInfo dictionary] setValue:[NSNumber numberWithBool:YES] forKey:NSPrintHeaderAndFooter];

    printOp = [NSPrintOperation printOperationWithView:textView printInfo:printInfo];
    [printOp setShowsPrintPanel:NO];
    [printOp runOperation];    
}


@implementation MyTextView 

- (NSAttributedString *)pageHeader
{
    // Adicionando cabeçalho
    NSAttributedString *theHeader = nil;

    NSImage * pic = [[NSImage alloc] initWithContentsOfFile:[dirLayout stringByAppendingString:@"cabecalho.jpg"]];
    NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    [attachment setAttachmentCell: attachmentCell ];
    theHeader = [NSAttributedString  attributedStringWithAttachment: attachment];
    return theHeader;
}

@end    

1 个答案:

答案 0 :(得分:1)

您应该覆盖-pageHeader,而不是覆盖-drawPageBorderWithSize:,这样您就可以在打印时在页面上绘制其他标记。

Size参数是一个NSSize结构,包含当前逻辑页面的大小。您需要做的就是在正确的位置绘制您的徽标:

- (void)drawPageBorderWithSize:(NSSize)pageSize
{
    [super drawPageBorderWithSize:pageSize];
    //draw your logo
    NSPoint offset = NSMakePoint(100.0, 100.0);
    NSImage* logo = [NSImage imageNamed:@"logo"];
    NSSize logoSize = [logo size];
    NSPoint imageOrigin = NSMakePoint(offset.x, pageSize.height - (offset.y + logoSize.height));
    [self lockFocus];
    [logo drawInRect:NSMakeRect(imageOrigin.x, imageOrigin.y, logoSize.width, logoSize.height)
            fromRect:NSZeroRect 
           operation:NSCompositeSourceOver
            fraction:1.0 
      respectFlipped:YES 
               hints:nil];
    [self unlockFocus];
}