我正在使用UIPrintPageRenderer子类在pdf上打印html内容。如何在打印内容(页眉和页脚)上添加水平线?
在UIPrintPageRenderer方法中,CGContextAddLineToPoint似乎不起作用。特别是那些用来绘制页眉和页脚的。NSString的drawAtPoint运行得很好。
这是我到目前为止尝试过的:
- (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect {
...
// Attempt 1 (Doesn't work!)
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1);
CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 1);
CGContextMoveToPoint(context, 10.0, 20.0);
CGContextAddLineToPoint(context, 310.0, 20.0);
// Attempt 2 (Doesn't work!)
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1);
CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 1);
CGContextTranslateCTM(context, 0, headerRect.size.height);
CGContextScaleCTM(context, 1, -1);
CGContextMoveToPoint(context, 10.0, 20.0);
CGContextAddLineToPoint(context, 310.0, 20.0);
}发布于 2011-07-14 15:15:41
因此,现在我应用了另一个解决方案。我仍然很想知道如何使用CGContext (无需加载图像)来实现这一点。这是我的解决方案:
// Draw horizontal ruler in the header
UIImage *horizontalRule = [UIImage imageNamed:@"HorizontalRule.png"];
horizontalRule = [horizontalRule stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
CGFloat rulerX = CGRectGetMinX(headerRect) + HEADER_LEFT_TEXT_INSET;
CGFloat rulerY = self.printableRect.origin.y + fontSize.height + HEADER_FOOTER_MARGIN_PADDING + PRINT_RULER_MARGIN_PADDING;
CGFloat rulerWidth = headerRect.size.width - HEADER_LEFT_TEXT_INSET - HEADER_RIGHT_TEXT_INSET;
CGFloat rulerHeight = 1;
CGRect ruleRect = CGRectMake(rulerX, rulerY, rulerWidth, rulerHeight);
[horizontalRule drawInRect:ruleRect blendMode:kCGBlendModeNormal alpha:1.0];发布于 2013-01-31 06:20:45
在Core Graphics中,逻辑图形元素被添加到上下文中,然后绘制。我看到您在上下文中添加路径,例如CGContextAddLineToPoint(context, 310.0, 20.0);
这将在内存中创建路径,但要将其合成到屏幕上,您需要填充或描边上下文的路径。尝试在后面添加CGContextStrokePath(context);以实际存储路径。
发布于 2015-06-01 15:01:59
就像常规绘图一样
- (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect {
CGContextRef context = UIGraphicsGetCurrentContext();
CContextMoveToPoint(context, CGRectGetMinX(headerRect), 70);
CGContextAddLineToPoint(context, CGRectGetMaxX(headerRect), 70);
CGFloat grayScale = 0.5f;
CGContextSetRGBStrokeColor(context, grayScale, grayScale, grayScale, 1);
CGContextStrokePath(context);
}别忘了CGStrokePath(...)
https://stackoverflow.com/questions/6688947
复制相似问题