iOS - 创建页面 pdf

标签 ios xcode pdf

以下代码来自iOS开发中心:http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html

问题是,事情看起来一团糟。例如,它在声明此类参数之前引用“Context”和“currentRange”。有人能从中弄清楚吗?也许我应该在其他地方声明它们,例如在“UIGraphicsBeginPDFPage”函数中?我确实知道您的 .m 文件中需要有“#import”。

- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRangeandFramesetter:(CTFramesetterRef)framesetter
{
// 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(Context, CGAffineTransformIdentity);

// Create a path object to enclose the text. Use 72 point
// margins all around the text.
CGRect    frameRect = CGRectMake(72, 72, 468, 648);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);

// Get the frame that will do the rendering.
// The currentRange variable specifies only the starting point. The framesetter
// lays out as much text as will fit into the frame.
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);

// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, 792);
CGContextScaleCTM(currentContext, 1.0, -1.0);

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

// Update the current range based on what was drawn.
currentRange = CTFrameGetVisibleStringRange(frameRef);
currentRange.location += currentRange.length;
currentRange.length = 0;
CFRelease(frameRef);

return currentRange;
}

- (void)drawPageNumber:(NSInteger)pageNum
{
NSString* pageString = [NSString stringWithFormat:@"Page %d", pageNum];
UIFont* theFont = [UIFont systemFontOfSize:12];
CGSize maxSize = CGSizeMake(612, 72);

CGSize pageStringSize = [pageString sizeWithFont:theFont
                               constrainedToSize:maxSize
                                   lineBreakMode:UILineBreakModeClip];
CGRect stringRect = CGRectMake(((612.0 - pageStringSize.width) / 2.0),
                               720.0 + ((72.0 - pageStringSize.height) / 2.0) ,
                               pageStringSize.width,
                               pageStringSize.height);

[pageString drawInRect:stringRect withFont:theFont];
}

最佳答案

currentRange 是该方法的一个参数。看起来您在那里丢失了一个空格:currentRangeandFramesetter应该是currentRange andFramesetter

Context 是一个拼写错误,应该是 currentContext。除此之外,代码可以编译。

关于iOS - 创建页面 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979798/

相关文章:

ios - 使用 PromiseKit 强制顺序下载

xcode - 是否有键盘快捷键可以清除 XCode 左侧导航器中的过滤器?

iphone - 核心数据内存分配

objective-c - 内存泄漏、nsmutablearray 和自定义对象

php - 使用 phpSpreadsheet 将 Excel 转换为 pdf

security - 如何实现 Eurion 星座安全措施

objective-c - 在具有 "dynamic prototypes"的静态 Tableview 中包含具有 "static cells"的部分

objective-c - 如何静态转储 Cocoa App 中调用的所有 ObjC 方法?

ios - 无法将类型 '__NSDictionaryM' 的值转换为 'NSArray"

pdf - 从 Swagger API 文档生成 PDF