【问题标题】:UITextView not showing on a generated PDFUITextView 未显示在生成的 PDF 上
【发布时间】:2012-07-25 16:15:01
【问题描述】:

我正在制作一个应用程序,用户可以在其中加载 pdf 并逐页查看并对其进行注释。用户可以在屏幕上绘制不同的颜色,还可以在屏幕上放置文本字段并放置其文本。

pdf 有一个视图,注释有一个视图。创建的 UITextViews 被添加为注释视图(代码中的绘图视图)的子视图。这些文本视图在应用程序中显示良好,但是当生成包含注释的 pdf 时,文本视图不显示(所有其他注释都显示)。

我试图用它自己的边界和其他视图使用的边界来调用 textView 的 drawRect,但都不起作用。绘图视图的唯一子视图是文本视图。

这里是我绘制生成 pdf 的地方:

for (int i = 0; i < numPages; i++) {
    UIGraphicsBeginPDFPage();

    //set the current pages of the views to get each page

    [self nextPage];

    [thePDFView drawRect:bounds];
    [theDrawingView drawRect:bounds];
    for(UITextView *txt in [theDrawingView subviews]){
        [txt drawRect:txt.bounds];
    }
}

UIGraphicsEndPDFContext();

关于如何在 pdf 上下文中绘制文本视图有什么想法吗?谢谢

编辑:最初是使用 UITextFields,但我不得不切换到 UITextViews 来解决不同的问题。 UITextViews 也不会显示在生成的 pdf 中。

【问题讨论】:

  • 放置 UIGraphicsBeginPDFPage(); for循环开始之前的行。我不确定。但是请您尝试检查一下。
  • 我得到了它的工作,这是应该进入循环 UITextViews 的 for 循环中的代码。 NSString *textToDraw = txt.text; UIFont *font = txt.font; CGRect renderingRect = txt.frame; [textToDraw drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
  • @mrunal BeginPDFPage() 调用必须进入 for 循环,因为这是我遍历 pdf 的每一页并将其绘制到 PDFContext 中的地方。请注意,for 循环上方是我在帖子中遗漏的 UIGraphicsBeginPDFContext()。
  • 嗯.. 很好,你得到了解决方案.. :)

标签: objective-c ios pdf pdf-generation uitextview


【解决方案1】:
for (int i = 0; i < numPages; i++) {
UIGraphicsBeginPDFPage();

//set the current pages of the views to get each page

[self nextPage];

[thePDFView drawRect:bounds];
[theDrawingView drawRect:bounds];
for(UITextView *txt in [theDrawingView subviews]){
    NSString *textToDraw = txt.text; 
    UIFont *font = txt.font; 
    CGRect renderingRect = txt.frame;

    [textToDraw drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
}

只是为了更好地格式化,这里是使 UITextView 在生成时呈现为 pdf 的代码。此方法似乎适用于呈现任何字符串对象,因此我确信它也适用于 UILabels 或 UITextFields。我使用 UITextiews 是因为它们很容易适应动态高度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多