我正在使用 UIPrintPageRenderer 子类在 pdf 上打印 html 内容。如何在打印内容(页眉和页脚)上添加水平线?
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);
}
最佳答案
在Core Graphics中,逻辑图形元素被添加到上下文中然后绘制。我看到您添加了一条到上下文的路径,例如CGContextAddLineToPoint(context, 310.0, 20.0);
这会在内存中创建路径,但要将其合成到屏幕上,您需要填充或描边上下文的路径。尝试添加CGContextStrokePath(context);
之后实际插入路径。
关于ios - 使用 UIPrintPageRenderer 在打印的 pdf 中绘制水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688947/