Cocoa-Created PDF无法正确呈现

时间:2010-04-25 06:41:26

标签: iphone cocoa pdf

我在iPad上创建了一个PDF,但问题是当你有一行大于1行的文本时,内容就会离开页面。这是我的代码:

void CreatePDFFile (CGRect pageRect, const char *filename) {
 CGContextRef pdfContext;
 CFStringRef path;
 CFURLRef url;
 CFMutableDictionaryRef myDictionary = NULL;
 path = CFStringCreateWithCString (NULL, filename,
           kCFStringEncodingUTF8);
 url = CFURLCreateWithFileSystemPath (NULL, path,
           kCFURLPOSIXPathStyle, 0);
 CFRelease (path);
 myDictionary = CFDictionaryCreateMutable(NULL, 0,
            &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks);
 NSString *foos = @"Title";
 const char *text = [foos UTF8String];
 CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR(text));
 CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("Author"));
 pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
 CFRelease(myDictionary);
 CFRelease(url);

 CGContextBeginPage (pdfContext, &pageRect);

 CGContextSelectFont (pdfContext, "Helvetica", 12, kCGEncodingMacRoman);
 CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
 CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
 NSString *body = @"text goes here";
 const char *text = [body UTF8String];
 CGContextShowTextAtPoint (pdfContext, 30, 750, text, strlen(text));

 CGContextEndPage (pdfContext);

 CGContextRelease (pdfContext);}

现在问题在于我将文本写入页面,但问题是我似乎无法指定多行“文本视图”。

1 个答案:

答案 0 :(得分:0)

我认为您可以在绘制文本时使用CGContextGetTextPosition来测量文本。使用它,您可以有效地包装文本。

您可以使用UILabel或UITextView绘制文本。将CALayer renderInContext与您的pdf上下文一起使用。

相关问题