[英]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.