如何在iOS7中绘制包含多条记录的PDF

时间:2013-10-21 08:14:35

标签: ios objective-c pdf ios7 quartz-core

我正在研究PDF生成功能,以下功能在iOS 6中正常工作但在iOS7中无效,所以请帮我解决同样的问题。

 (float) drawText:(NSString *)body currentVehical:(NSInteger)currentVehical{

    CGContextRef    currentContext = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

    UIFont *font = [UIFont systemFontOfSize:14.0];

    NSLog(@" FRame : \n%f\n%f",(self.pageSize.width - 2*kBorderInset-2*kMarginInset), 

(self.pageSize.height - 2 * kBorderInset - 2 * kMarginInset));

    CGSize stringSize = [body sizeWithFont:font
                         constrainedToSize:CGSizeMake(self.pageSize.width - 2*kBorderInset-2*kMarginInset, self.pageSize.height - 2*kBorderInset - 2*kMarginInset)
                             lineBreakMode:NSLineBreakByWordWrapping];


    CGRect renderingRect = CGRectMake(120,10+self.yCord, self.pageSize.width - 120,stringSize.height+KExtraSpaces);

    NSLog(@"PDF draw rect: %@",NSStringFromCGRect(renderingRect));

    if (IS_IOS7) {
        NSMutableParagraphStyle *paragraphstyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        paragraphstyle.lineBreakMode = NSLineBreakByWordWrapping;
        paragraphstyle.alignment = NSTextAlignmentLeft;
        NSDictionary * attributes = @{ NSFontAttributeName:font, NSParagraphStyleAttributeName: paragraphstyle };
            //[body drawInRect:renderingRect withAttributes:attributes];
        NSStringDrawingContext *context = [NSStringDrawingContext new];
        context.minimumScaleFactor = 0.1;
        NSLog(@"body :%@",body);
        [body drawWithRect:renderingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:context];
    }else{

    [body drawInRect:renderingRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
    }
    return stringSize.height;
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

我记得在为iOS7编译时,pdf渲染器存在类似的问题,当它完美地适用于ios6.1时。我似乎记得字体大小是问题,调试您的例程并检查字体大小和后续帧大小。道歉,我不能更具体,因为3个月前已经解决并被遗忘。

希望此代码有所帮助:

+(void)drawTableDataAt:(Survey*)survey columns:(NSMutableArray*)savedColumns listContent:(NSArray*)surveyListContent withOrigin:(CGPoint)origin andRowCount:(int*)count andMaxRowOnPage:(int)size andLinesPerPage:(int)linesPerPage andColumnWidths:(NSMutableArray*)columnWidths withFont:(UIFont*)theFont withLabelFont:(UIFont*)theLabelFont
{
float rowHeight = theFont.lineHeight+3.0;
float padding = -5.0f;

// ………..

CGRect frame = CGRectMake(originX + padding, originY, [[columnWidths objectAtIndex:j] integerValue], rowHeight);
[self drawText:strValue inFrame:frame withFont:theFont   withAlignment:kCTRightTextAlignment];

// ………..
}

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect withFont:(UIFont*)theFont withAlignment:(CTTextAlignment)textAlignment
{

CFStringRef stringRef = (__bridge_retained CFStringRef)textToDraw;
NSString *fontName = [theFont fontName];
CGFloat fontSize = [theFont pointSize];
// Set Font
CTFontRef font = CTFontCreateWithName( (__bridge CFStringRef)fontName, fontSize, NULL);
CTParagraphStyleSetting settings[ASD_ParagraphStylesSupported];

settings[0].spec = kCTParagraphStyleSpecifierAlignment;
settings[0].valueSize = sizeof(CTTextAlignment);
settings[0].value = &textAlignment;

CTParagraphStyleRef style = CTParagraphStyleCreate((const CTParagraphStyleSetting*) &settings, ASD_ParagraphStylesSupported);

// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName, kCTParagraphStyleAttributeName };
CFTypeRef values[] = { font, [[UIColor blackColor] CGColor], style };
CFDictionaryRef attrs = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
                                           sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// Prepare the text using a Core Text Framesetter with Attributes
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, attrs);

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.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);

// Draw the frame.
CTFrameDraw(frameRef, currentContext);

CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);

CFRelease(settings);
CFRelease(attrs);
CFRelease(style);
CFRelease(font);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
}