我已经制作了一个非常适合 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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)