簡體   English   中英

可可打印

[英]Printing in Cocoa

我做了一個非常適合 A4 頁面的視圖。 現在我想打印它。 請注意,我沒有使用 drawRect 或類似的東西,只是一個帶有子視圖和文本標簽的普通視圖。 我的問題是我在該視圖上有一些視圖,我使用圖層在項目周圍放置背景顏色和圓角矩形。 子視圖不打印,但所有文本標簽打印。

_printReport 只是一個帶有視圖和一堆文本標簽的普通窗口。

我做錯了什么,我怎么能做得更好? 我真的不想做一個drawrect,但如果必須的話我會。

這是有人打印時發生的代碼:

- (void)printWorksheet {
    TJContact *worksheet = [[self.workSheets selectedObjects] objectAtIndex:0];
    if (worksheet == nil) return;

    _printReport = [[TJPrintReportWindowController alloc] initWithWindowNibName:@"TJPrintReportWindowController"];
    [self.printReport setCompany:self.company];
    [self.printReport setContact:worksheet];

    [self.printReport showWindow:self];
    [self.printReport becomeFirstResponder];
    [self.printReport.view becomeFirstResponder];
    NSPrintInfo* printInfo = [NSPrintInfo sharedPrintInfo];

    [printInfo setHorizontalPagination:NSFitPagination];
    [printInfo setVerticalPagination:NSFitPagination];
    [printInfo setHorizontallyCentered:YES];
    [printInfo setVerticallyCentered:YES];
    [printInfo setLeftMargin:20.0];
    [printInfo setRightMargin:20.0];
    [printInfo setTopMargin:10.0];
    [printInfo setBottomMargin:10.0];

    NSPrintOperation* printOperation = [NSPrintOperation printOperationWithView:self.printReport.view printInfo:printInfo];
    [printOperation setShowsPrintPanel:YES];
    [printOperation runOperationModalForWindow:[self window] delegate:nil didRunSelector:nil contextInfo:nil];
}

不確定這是否有幫助,但主視圖確實將 setWantsLayers 設置為 YES,這是我的裝飾之一:

CALayer *customerLayer = [self.customerView layer];
[customerLayer setCornerRadius:10];
[customerLayer setBackgroundColor:[NSColor colorWithDeviceWhite:0 alpha:0.30].CGColor];
[customerLayer setBorderColor:[NSColor blackColor].CGColor];
[customerLayer setBorderWidth:1.5];

當我在屏幕上顯示窗口時,它看起來就像我想要的那樣,但是上面的圓形矩形沒有被打印出來,但是它上面的所有標簽都可以。

有時,通過自定義繪圖來重現圖層提供的簡單效果會更容易。 但有時這是一種真正的痛苦。 要打印圖層支持的視圖,您可以先將它們渲染為圖像,然后打印 NSImageView。

我給你一個通用的解決方案,將整個 NSView 及其所有子視圖和子層層次結構轉換為 NSImageView。 請注意,這是使用 MonoMac 用 c# 編寫的。 您應該能夠輕松地將其轉換為 objC。 嘗試在 Layer.RenderInContext 上搜索示例以獲取 objC 中的進一步參考。 下面的方法適用於復雜的視圖層次結構(例如:包含半透明層支持的子視圖的表視圖在其行視圖中具有圓角):

//convert printView (your ready-to-print view which contains layer backed views) to a image view 
{
    //first we enclose the view in a new NSView - this fixes an issue which prevents the view to print upside down in some cases - for instance if printView is a NSScrollView, it will be drawn upside down
    NSView container = new NSView (printView.Frame);
    container.AddSubview (printView);
    printView = container;

    printView.WantsLayer = true;
    RectangleF bounds = printView.Bounds;
    int bitmapBytesPerRow = 4 * (int)bounds.Size.Width;
        using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB())
    using (CGBitmapContext context = new CGBitmapContext (null,
                                         (int)bounds.Width,
                                         (int)bounds.Height,
                                         8,
                                         bitmapBytesPerRow,
                                         colorSpace,
                                         CGImageAlphaInfo.PremultipliedLast)) {
        printView.Layer.RenderInContext (context);
        NSImageView canvas = new NSImageView (bounds);
        canvas.Image = new NSImage (context.ToImage (), bounds.Size); //instead of context.ToImage() in objC you have CGBitmapContextCreateImage
        printView = canvas;
    }
}

打印時不會出現圖層。 我必須制作自己的 NSView 對象並執行 drawRect 函數以放置為背景視圖。 然后它自動打印出來,什么也不做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM