gpt4 book ai didi

ios - 如何在 iOS 上生成文本内容为 "real"的 PDF?

转载 作者:可可西里 更新时间:2023-11-01 03:33:14 25 4
gpt4 key购买 nike

我想在我的 iOS 6 应用程序中生成一个好看的 PDF。

我试过:

  • UIView 在上下文中呈现
  • 使用核心文本
  • 使用 NSString drawInRect
  • 使用 UILabel drawRect

这是一个代码示例:

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(NSString *) path
{
CGContextRef myOutContext = NULL;
NSURL * url;

url = [NSURL fileURLWithPath:path];
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL ((__bridge CFURLRef) url,
&inMediaBox,
NULL);
}


return myOutContext;
}

-(void)savePdf:(NSString *)outputPath
{
if (!pageViews.count)
return;

UIView * first = [pageViews objectAtIndex:0];

CGContextRef pdfContext = [self createPDFContext:CGRectMake(0, 0, first.frame.size.width, first.frame.size.height) path:outputPath];

for(UIView * v in pageViews)
{
CGContextBeginPage (pdfContext,nil);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, (int)(v.frame.size.height));
transform = CGAffineTransformScale(transform, 1, -1);
CGContextConcatCTM(pdfContext, transform);

CGContextSetFillColorWithColor(pdfContext, [UIColor whiteColor].CGColor);
CGContextFillRect(pdfContext, v.frame);

[v.layer renderInContext:pdfContext];

CGContextEndPage (pdfContext);
}

CGContextRelease (pdfContext);
}

渲染的 UIView 只包含一个 UIImageView + 一堆 UILabel(有的有边框,有的没有边框)。

我还尝试了在 stackoverflow 上找到的建议:子类化 UILabel 并执行此操作:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
BOOL isPDF = !CGRectIsEmpty(UIGraphicsGetPDFContextBounds());
if (!layer.shouldRasterize && isPDF)
[self drawRect:self.bounds]; // draw unrasterized
else
[super drawLayer:layer inContext:ctx];
}

但这也没有改变任何东西。

无论我做什么,在预览中打开 PDF 时,文本部分都可以作为一个 block 选择,但不能选择每个字符的字符,并且缩放 pdf 显示它实际上是一个位图图像。

有什么建议吗?

最佳答案

关于ios - 如何在 iOS 上生成文本内容为 "real"的 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12556244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com