2011-07-14 19 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(...)