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

标签 objective-c cocoa nstextview

我正在从 NSTextview 保存 PDF 并将 Logo 放入标题中。我覆盖了 pageHeader 并且出现了 Logo ,但它被剪掉了。

是否可以更改 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    

最佳答案

您不应覆盖 -pageHeader,而应覆盖 -drawPageBorderWithSize:,这样您可以在打印时在页面上绘制附加标记。

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

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

关于objective-c - 是否可以更改 NSTextView 的标题高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9038240/

相关文章:

iphone - 在 didSelectRowAtIndexPath 访问自定义标签属性

cocoa - 绑定(bind) NSMenuItem 的标题会破坏启用/禁用验证

cocoa - 令人难以置信的 QTMovie 导出崩溃

objective-c - 未调用 NSTextField 自动完成委托(delegate)方法

objective-c - 在 NSTextView 中创建指向某些文本的超链接

ios - UIView 插入 UIWebView 的 ScrollView ,在横向模式下不可点击

objective-c - 使用 ARC 在 block 中设置 NSError

ios - 如何通过 Objective-C 中的引导访问检测硬件按钮是否被禁用?

swift - 在MacOs App中使用Wkwebview的Facebook手动登录流程失败

macos - 更改 NSTextView 的大小