使用 CGContext 以编程方式创建 PDF - 无法换行 + iPhone
我正在使用 CGContext 和 CGContextShowTextAtPoint 在我的 iPhone 应用程序中以编程方式创建 PDF。虽然这对于较小的文本效果很好,但每当我的文本中有换行符 (\n) 或希望文本在到达页面末尾时自动换行到下一行时,这种情况就不会发生。
文本中的任何换行符都会简单地替换为空格,并且文本不会换行。如果能了解如何实现这一目标,我真的很感激。这是我用来创建 PDF 的代码。
void CreatePDFFile (CGRect pageRect, const char *filename, NSString *fromFile) {
CGContextRef pdfContext;
CFStringRef path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
CGURLRef url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease (path);
CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
CFRelease(myDictionary);
CFRelease(url);
CGContextBeginPage (pdfContext, &pageRect);
CGContextSelectFont (pdfContext, "Helvetica", 14, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 150, 1);
/* This works
const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));*/
//This doesn't print line breaks in the PDF
const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem\n accusantium doloremque laudantium, totam rem aperiam, eaque ipsa";
CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));
//This doesn't wrap text to the next line
const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa";
CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));
CGContextEndPage (pdfContext);
CGContextRelease (pdfContext);
}
I am creating a PDF programmatically in my iPhone app using CGContext and CGContextShowTextAtPoint. While this works fine for smaller text, whenever I have line breaks in my text (\n) or want the text to wrap automatically to the next line when it reaches the end of the page, this does not happen.
Any line breaks in the text are simply replaced by spaces and the text does not wrap. Would really appreciate a heads-up on how this can be achieved. Here's the code I use to create the PDF.
void CreatePDFFile (CGRect pageRect, const char *filename, NSString *fromFile) {
CGContextRef pdfContext;
CFStringRef path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
CGURLRef url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease (path);
CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
CFRelease(myDictionary);
CFRelease(url);
CGContextBeginPage (pdfContext, &pageRect);
CGContextSelectFont (pdfContext, "Helvetica", 14, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 150, 1);
/* This works
const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));*/
//This doesn't print line breaks in the PDF
const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem\n accusantium doloremque laudantium, totam rem aperiam, eaque ipsa";
CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));
//This doesn't wrap text to the next line
const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa";
CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));
CGContextEndPage (pdfContext);
CGContextRelease (pdfContext);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CGContextShowTextAtPoint
仅进行基本的文本绘制。您应该能够使用 UIKit 添加NSString 用于诸如包装之类的事情。例如:但请注意,
NSString
方法作用于当前上下文,因此您需要将上下文设为当前上下文才能使用它们。我想如果您在后台线程或其他线程中执行此操作可能会出现问题。CGContextShowTextAtPoint
only does basic text drawing. You should be able to use the UIKit additions to NSString for things like wrapping. Eg:Note, though, that the
NSString
methods act on the current context, so you will need to make your context current in order to use them. I guess might be a problem if you're doing this in a background thread or something.