0

我正在开发以下功能的 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;
}

提前致谢。

4

1 回答 1

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);
}
于 2013-10-22T01:51:40.760 回答