Ios 使用UIPrintPageRenderer在打印pdf中绘制水平线
我使用UIPrintPageRenderer子类在pdf上打印html内容。如何在打印内容(页眉和页脚)上添加水平线Ios 使用UIPrintPageRenderer在打印pdf中绘制水平线,ios,printing,drawing,uiprintpagerenderer,Ios,Printing,Drawing,Uiprintpagerenderer,我使用UIPrintPageRenderer子类在pdf上打印html内容。如何在打印内容(页眉和页脚)上添加水平线 CGContextAddLineToPoint在UIPrintPageRenderer方法中似乎不起作用。特别是那些用来画页眉和页脚的。NSString的drawAtPoint工作正常 以下是我迄今为止所尝试的: - (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect {
CGContextAddLineToPoint
在UIPrintPageRenderer方法中似乎不起作用。特别是那些用来画页眉和页脚的。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);
}
所以,现在我已经应用了另一种解决方案。我仍然很想知道如何使用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];
在核心图形中,逻辑图形元素添加到上下文中,然后绘制。我看到您使用例如
CGContextAddLineToPoint(context,310.0,20.0)向上下文添加路径代码>
这将在内存中创建路径,但要将其合成到屏幕,您需要填充或笔划上下文的路径。尝试添加CGContextStrokePath(上下文)编码>之后,实际为路径加上斯托克。与常规绘图一样
- (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(…)
我们可以使用自定义UI吗,UI在标题部分包含两个UIElements,?