ios - 使用 UIPrintPageRenderer 在打印的 pdf 中绘制水平线

标签 ios printing drawing uiprintpagerenderer

我正在使用 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/

相关文章:

ios - 如何从 CLLocation 变量中提取经度和纬度?

ios - 如何在自己的行上制作每个customLabel?

ios - 获取 3D 模型的高度及其转换后的屏幕坐标

python - 在python中递归打印钻石

objective-c - UIImageView 上的 roundedCorner 错误地应用于 UITableCell

Python 打印到/dev/usb/lp0

java - 在 java 中打印将标题设置为 "Java Printing"

ios - 育儿时如何覆盖CALayers的绘图顺序

ios - 在 UIScrollView 的 subview 中绘制网格分配大量内存

关于允许用户画线的 Java 挑战