2011-07-14 21 views
3

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

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); 
} 

回答

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]; 
1

在核心图形,逻辑图形元素被添加到上下文,然后绘制。我看到你用上下文添加了一个上下文路径。 CGContextAddLineToPoint(context, 310.0, 20.0);

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

0

正如常规绘图

- (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(...)