首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用UIPrintPageRenderer在打印的pdf中绘制水平线

用UIPrintPageRenderer在打印的pdf中绘制水平线
EN

Stack Overflow用户
提问于 2011-07-14 13:46:49
回答 3查看 1.6K关注 0票数 3

我正在使用UIPrintPageRenderer子类在pdf上打印html内容。如何在打印内容(页眉和页脚)上添加水平线?

在UIPrintPageRenderer方法中,CGContextAddLineToPoint似乎不起作用。特别是那些用来绘制页眉和页脚的。NSString的drawAtPoint运行得很好。

这是我到目前为止尝试过的:

代码语言:javascript
复制
- (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);
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-07-14 15:15:41

因此,现在我应用了另一个解决方案。我仍然很想知道如何使用CGContext (无需加载图像)来实现这一点。这是我的解决方案:

代码语言:javascript
复制
// 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];
票数 0
EN

Stack Overflow用户

发布于 2013-01-31 06:20:45

在Core Graphics中,逻辑图形元素被添加到上下文中,然后绘制。我看到您在上下文中添加路径,例如CGContextAddLineToPoint(context, 310.0, 20.0);

这将在内存中创建路径,但要将其合成到屏幕上,您需要填充或描边上下文的路径。尝试在后面添加CGContextStrokePath(context);以实际存储路径。

票数 1
EN

Stack Overflow用户

发布于 2015-06-01 15:01:59

就像常规绘图一样

代码语言:javascript
复制
- (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(...)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6688947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档