I have a project in which I am creating a PDF. I have a xib, "PDFView" which lays out the background image and labels. Everything else is drawn on the PDF from my PDFRenderer class which is a NSObject class. I am trying to make it so the text shown in the labels is data previously entered by a user on a previous screen. Here is my code:
PDFRenderer.h
#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>
#import "Storage.h"
@interface PDFRenderer : NSObject
+(void)drawPDF:(NSString*)fileName;
+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect;
+(void)drawLogo;
@end
PDFRenderer.m
#import "PDFRenderer.h"
@implementation PDFRenderer
+(void)drawLabels
{
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"PDFView" owner:nil options:nil];
UIFont *font = [UIFont systemFontOfSize:8];
NSDictionary *text = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,
nil];
UIView* mainView = [objects objectAtIndex:0];
for (UIView* view in [mainView subviews]) {
if([view isKindOfClass:[UILabel class]])
{
UILabel* label = (UILabel*)view;
[label.text drawInRect:label.frame withAttributes: text];
}
}
}
+(void)drawImage:(UIImage*)image inRect:(CGRect)rect
{
[image drawInRect:rect];
}
+(void)drawLogo
{
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"PDFView" owner:nil options:nil];
UIView* mainView = [objects objectAtIndex:0];
for (UIView* view in [mainView subviews]) {
if([view isKindOfClass:[UIImageView class]])
{
UIImage* logo = [UIImage imageNamed:@"JmHaloReportImage.png"];
[self drawImage:logo inRect:view.frame];
}
}
}
+(void)drawPDF:(NSString*)fileName
{
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
[self drawLogo];
[self drawLabels];
// Close the PDF context and write out the contents.
UIGraphicsEndPDFContext();
}
+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect
{
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter.
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
// Modify this to take into consideration the origin.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2 + frameRect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
// Add these two lines to reverse the earlier transformation.
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*(frameRect.origin.y*2 + frameRect.size.height));
CFRelease(frameRef);
//CFRelease(stringRef);
CFRelease(framesetter);
}
@end
Each of my labels in the xib has a separate tag. I don't understand how to reference each individual label by tag. Any assistance? I am completely lost and trying to learn.