我现在在 iOS 的打印界面上苦苦挣扎。我拥有的是一个带有自定义 UIView 的视图控制器,它在 drawRect 中进行绘图:
此处为绘图在 iPad 上的外观示例(包括一些彩色测试线):
我设置了一个 UIPrintRenderer 子类,它打印页眉和页脚,并为自定义 UIView 的 drawRect: 方法提供内容矩形 drawContentForPageAtIndex:inRect:。调试器告诉我,drawRect: 总是被调用——不管 rect drawContentForPageAtIndex:inRect: 是否存在于渲染器类中。所以我不在 rect drawContentForPageAtIndex:inRect: 中进行绘图,我使用 drawRect: 。
当打印机模拟器提供 A4 纸张格式时,我得到 [0, 0, 841.89, 595.276] 的纸张矩形,[11.9905, 11.9906, 817.909, 571.294] 的可打印矩形和 [11.9905, 33.9906, 817.909, 530.294] 的内容矩形]。我确实不明白的是, drawRect: 从打印界面获取 [0, 0, 695, 530.294] 的矩形。那是内容矩形的高度,但宽度是正常绘制时不打印时从矩形开始的高度:[0, 0, 695, 648]。在模拟器输出页面是这样的(当然也添加了一些测试帧、线和圆):
下面是一些代码,首先是视图控制器中设置打印的部分:
- (IBAction)pressedPrint:(id)sender
{
UIPrintInteractionController* printCtrl = [UIPrintInteractionController sharedPrintController];
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
NSLog(@"%s prepare printing parameters etc.", __func__);
printCtrl.delegate = self;
printInfo.outputType = UIPrintInfoOutputPhoto;
printInfo.orientation = UIPrintInfoOrientationLandscape;
printInfo.jobName = [NSString stringWithFormat:@"%@ - %@", NSLocalizedString(@"NavTitleMain", @""), self.title];
printInfo.duplex = UIPrintInfoDuplexNone;
printCtrl.printInfo = printInfo;
printCtrl.showsPageRange = NO;
// This code uses a custom UIPrintPageRenderer so that it can draw a header and footer.
DVPrintPageRenderer* myRenderer = [[DVPrintPageRenderer alloc] init];
// The DVPrintPageRenderer class provides a jobtitle that it will label each page with.
myRenderer.jobTitle = printInfo.jobName;
myRenderer.isTwoPageView = NO;
myRenderer.footerText = [centralDocument sharedInstance].docTitle;
myRenderer.drawView = self.drawArea;
// To draw the content of each page, a UIViewPrintFormatter is used.
UIViewPrintFormatter* viewFormatter = [self.drawArea viewPrintFormatter];
UIFont* titleFont = [UIFont fontWithName:@"Helvetica" size:HEADER_TEXT_HEIGHT];
CGSize titleSize = [myRenderer.jobTitle getSizeWithFont:titleFont];
UIFont* footerFont = [UIFont fontWithName:@"Helvetica" size:FOOTER_TEXT_HEIGHT];
CGSize footerSize = [myRenderer.footerText getSizeWithFont:footerFont];
viewFormatter.startPage = 0;
myRenderer.headerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;
myRenderer.footerHeight = footerSize.height + HEADER_FOOTER_MARGIN_PADDING;
[myRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
// Set our custom renderer as the printPageRenderer for the print job.
printCtrl.printPageRenderer = myRenderer;
drawArea.isPrinting = YES;
drawArea.printRenderer = myRenderer;
NSLog(@"%s before presenting the printer dialog", __func__);
void (^completionHandler)(UIPrintInteractionController*, BOOL, NSError*) =
^(UIPrintInteractionController* printController, BOOL completed, NSError* error)
{
drawArea.isPrinting = NO;
drawArea.printRenderer = nil;
if (!completed && error)
{
NSLog(@"Printing could not complete because of error: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[printCtrl presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
}
else
{
[printCtrl presentAnimated:YES completionHandler:completionHandler];
}
}
drawRect: 的第一个代码部分也可能与此处相关:
- (void)drawRect: (CGRect)rect
{
// Drawing code.
NSLog(@"%s entered for %@", __func__, (isPrinting ? @"printing" : @"screen drawing"));
NSInteger colorCount = (colorArray == nil) ? -1 : [colorArray count];
NSInteger graphCount = (graphPoints == nil) ? -1 : [graphPoints count];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
if (isPrinting)
{
// make sure that we'll print the frame rectangle
CGRect newRect = CGRectInset(printRenderer.rectContent, 2.0, 2.0);
saveGraphRect = drawGraphRect;
saveBackColor = viewBackColor;
saveTextColor = viewBackColor;
CGContextTranslateCTM(context, printRenderer.rectContent.origin.x, -printRenderer.rectContent.origin.y);
NSLog(@"%s content = [%g, %g, %g, %g]", __func__,
printRenderer.rectContent.origin.x, printRenderer.rectContent.origin.y,
printRenderer.rectContent.size.width, printRenderer.rectContent.size.height);
NSLog(@"%s rect(1) = [%g, %g, %g, %g]", __func__,
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
rect = CGRectMake(ceilf(newRect.origin.x), ceilf(newRect.origin.y),
floorf(newRect.size.width), floorf(newRect.size.height));
CGContextTranslateCTM(context, rect.origin.x, rect.origin.y);
rect.origin = CGPointMake(0.0, 0.0);
usedBounds = rect;
drawGraphRect = [self makeInsetDrawRectFrom:rect];
viewBackColor = [UIColor whiteColor];
axisTextColor = [UIColor blackColor];
NSLog(@"%s prepared for printing", __func__);
NSLog(@"%s bounds = [%g, %g, %g, %g]", __func__,
self.bounds.origin.x, self.bounds.origin.y,
self.bounds.size.width, self.bounds.size.height);
NSLog(@"%s paper = [%g, %g, %g, %g]", __func__,
printRenderer.paperRect.origin.x, printRenderer.paperRect.origin.y,
printRenderer.paperRect.size.width, printRenderer.paperRect.size.height);
NSLog(@"%s rect(2) = [%g, %g, %g, %g]", __func__,
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
NSLog(@"%s draw = [%g, %g, %g, %g]", __func__,
drawGraphRect.origin.x, drawGraphRect.origin.y, drawGraphRect.size.width, drawGraphRect.size.height);
}
else
{
usedBounds = self.bounds;
...
drawRect: 中矩形的 NSLog 输出也相关:
2014-01-21 17:15:39.906 iELANA[19015:a0b] -[DrawView drawRect:] entered for screen drawing
2014-01-21 17:15:39.906 iELANA[19015:a0b] -[DrawView drawRect:] bounds = [0, 0, 695, 648]
2014-01-21 17:15:39.906 iELANA[19015:a0b] -[DrawView drawRect:] rect = [0, 0, 695, 648]
2014-01-21 17:15:39.906 iELANA[19015:a0b] -[DrawView drawRect:] draw = [62, 2, 631, 584]
2014-01-21 17:15:40.645 iELANA[19015:a0b] -[viewDiagram pressedPrint:] prepare printing parameters etc.
2014-01-21 17:15:40.645 iELANA[19015:a0b] -[viewDiagram pressedPrint:] before presenting the printer dialog
2014-01-21 17:15:46.131 iELANA[19015:a0b] printableRect = [11.9905, 11.9906, 817.909, 571.294]
2014-01-21 17:15:46.131 iELANA[19015:a0b] headerRect = [11.9905, 11.9906, 817.909, 22]
2014-01-21 17:15:46.131 iELANA[19015:a0b] header text size = [297, 17]
2014-01-21 17:15:46.133 iELANA[19015:a0b] -[DVPrintPageRenderer drawContentForPageAtIndex:inRect:] page = 0, rect [11.9905, 33.9906, 817.909, 530.294]
2014-01-21 17:15:46.133 iELANA[19015:a0b] paper rect = [0, 0, 841.89, 595.276]
2014-01-21 17:15:46.133 iELANA[19015:a0b] printable rect = [11.9905, 11.9906, 817.909, 571.294]
2014-01-21 17:15:46.133 iELANA[19015:a0b] -[DrawView drawRect:] entered for printing
2014-01-21 17:15:46.134 iELANA[19015:a0b] -[DrawView drawRect:] content = [11.9905, 33.9906, 817.909, 530.294]
2014-01-21 17:15:46.134 iELANA[19015:a0b] -[DrawView drawRect:] rect(1) = [0, 0, 695, 530.294]
2014-01-21 17:15:46.134 iELANA[19015:a0b] -[DrawView drawRect:] prepared for printing
2014-01-21 17:15:46.134 iELANA[19015:a0b] -[DrawView drawRect:] bounds = [0, 0, 695, 648]
2014-01-21 17:15:46.134 iELANA[19015:a0b] -[DrawView drawRect:] paper = [0, 0, 841.89, 595.276]
2014-01-21 17:15:46.134 iELANA[19015:a0b] -[DrawView drawRect:] rect(2) = [0, 0, 813, 526]
2014-01-21 17:15:46.135 iELANA[19015:a0b] -[DrawView drawRect:] draw = [62, 2, 749, 462]
2014-01-21 17:15:46.140 iELANA[19015:a0b] printableRect = [11.9905, 11.9906, 817.909, 571.294]
2014-01-21 17:15:46.140 iELANA[19015:a0b] footerRect = [11.9905, 564.285, 817.909, 19]
2014-01-21 17:15:46.140 iELANA[19015:a0b] footer text size = [296, 14]
绘图代码使用 Core Graphics 和 UIKit 来绘制框架、线条等。文本输出使用 Core Text。因此,应用正确的 CTM 转换仍有一些工作要做。但主要的第一个问题是:
为什么 drawRect: 从打印界面获取一个奇怪的矩形而不是内容矩形?如果我不能打破这条规则,那么 iPhone 打印而不是 iPad 会变得非常糟糕——使用当前编码的 iPhone 看起来更糟。
我是否应该在打印渲染器的 drawContentForPageAtIndex:inRect: 中更好地执行打印绘图代码?
非常感谢您的帮助:-)
康兰
添加:
似乎确实如此,传递给 drawRect: 的图形上下文不适用于打印要求。当我直接从drawRect返回时:在打印的情况下并将打印代码添加到drawContentForPageAtIndex:inRect:,我得到一个更好的(测试)输出:
但是我不明白并且仍然需要您帮助的是轴文本的定位代码,这些代码是使用 Core Text 绘制的。我尝试了非常不同的 CTM 转换组合......但我使用的任何组合都会导致文本定位错误。这就是我为 x 轴上的文本输出所做的:
- (void)drawAxisValueXin: (CGContextRef)context withText: (NSString*)axisValueX dockToPoint: (CGPoint)dockPoint
{
BOOL isRetina = (!isPrinting && AfxGetApp().isRetina);
CGFloat rMul = (isRetina) ? 2.0 : 1.0;
CGFloat fontSize = 10.0;
CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), fontSize, NULL);
if (isRetina)
{
dockPoint.x *= 2.0;
dockPoint.y *= 2.0;
}
// flip the coordinate system
CGContextSaveGState(context);
if (isPrinting)
{
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0.0f, CGRectGetHeight(printRenderer.paperRect));
CGContextScaleCTM(context, 1.0f, -1.0f);
}
else
{
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0.0f, CGRectGetHeight(usedBounds) * rMul);
CGContextScaleCTM(context, 1.0f, -1.0f);
}
// make the attributed string
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:axisValueX];
NSRange strRange = NSMakeRange(0, [attrString length]);
[attrString addAttribute: (id)kCTFontAttributeName
value: (__bridge id)helvetica
range: strRange];
[attrString addAttribute: (id)kCTForegroundColorAttributeName
value: (id)axisTextColor.CGColor
range: strRange];
// draw the text
CTLineRef ctLine = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
CGFloat ascent;
CGFloat descent;
CGFloat width = ceilf(CTLineGetTypographicBounds(ctLine, &ascent, &descent, NULL));
CGFloat height = ceilf(ascent + descent);
CGSize textSize = CGSizeMake(width, height);
CGPoint atPoint = CGPointMake(dockPoint.x + (1.0 - textSize.width) * rMul,
dockPoint.y + (10.0 - 2.0) * rMul); // x = text right edge, y = text mid
CGPoint userPoint = CGContextConvertPointToUserSpace(context, atPoint);
if (fabsf(tiltLeftAxisX) > 1.0e-8)
{
CGContextRotateCTM(context, -tiltLeftAxisX);
dockPoint.y += 2.0 * rMul; // offset y 2 points down
dockPoint = CGContextConvertPointToUserSpace(context, dockPoint);
dockPoint.x = nearbyintf(dockPoint.x);
dockPoint.y = nearbyintf(dockPoint.y);
userPoint = CGPointMake(dockPoint.x - width,
dockPoint.y - height / 2.0);
}
CGContextSetTextPosition(context, userPoint.x, userPoint.y);
CTLineDraw(ctLine, context);
CGRect rectTempl = CGRectMake(-5.0, -5.0, 10.0, 10.0);
CGRect rectDot = CGRectOffset(rectTempl, userPoint.x, userPoint.y);
[[UIColor redColor] set];
CGContextFillEllipseInRect(context, rectDot);
rectDot = CGRectOffset(rectTempl, atPoint.x, atPoint.y);
[[UIColor greenColor] set];
CGContextFillEllipseInRect(context, rectDot);
// clean up
CFRelease(ctLine);
CFRelease(helvetica);
CGContextRestoreGState(context);
}