UITextView未显示在生成的PDF上

时间:2012-07-25 16:15:01

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

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

pdf有一个视图,注释有一个视图。创建的UITextViews被添加为注释视图的子视图(代码中的DrawingView)。这些文本视图在应用程序中显示正常,但是当生成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上显示。

1 个答案:

答案 0 :(得分: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或UITextField也可以正常工作。我使用UITextiews因为它们很容易适应动态高度。