Iphone 需要将UITextView完整内容呈现为PDF

Iphone 需要将UITextView完整内容呈现为PDF,iphone,objective-c,ios,pdf,uitextview,Iphone,Objective C,Ios,Pdf,Uitextview,在@implementation UIView(SaveToPdf)类中,我有以下方法可以很好地工作 我使用这种方法将我的视图打印成PDF,除了一个问题外,效果很好。我的视图包含UITextView,pdf仅打印该UITextView的可见区域。我需要PDF来呈现完整内容(即可滚动文本) 我是iOS开发新手,如果有人能为我指出正确的方向,我将不胜感激 *需要注意的一点是,UIView中还有其他子视图(标签、文本字段等)也需要打印到PDF中。这目前工作正常,仅使用[self.layer rende

在@implementation UIView(SaveToPdf)类中,我有以下方法可以很好地工作

我使用这种方法将我的视图打印成PDF,除了一个问题外,效果很好。我的视图包含UITextView,pdf仅打印该UITextView的可见区域。我需要PDF来呈现完整内容(即可滚动文本)

我是iOS开发新手,如果有人能为我指出正确的方向,我将不胜感激

*需要注意的一点是,UIView中还有其他子视图(标签、文本字段等)也需要打印到PDF中。这目前工作正常,仅使用[self.layer renderInContext:pdfContext]即可将布局保存在我的PDF中;由于主视图将在其所有子视图上循环


谢谢

我知道两种方法-

  • 我见过一些代码,其中
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]正在完成。当我尝试时,我发现这会导致我的文字清晰度下降。所以我不能用这个方法
  • 这是比较传统的方法。i、 提取文本并绘制。这段代码坚如磐石,已经有了一些用法。因此,您需要做的就是从
    uitextview
    获取所有文本,并将其传递到此处
  • 希望这有帮助

    #define kBorderInset 25.0
    #define kMarginInset 15.0
    
    - (void) drawText
    {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
    
    NSString *textToDraw = @"YOUR TEXT FROM UITEXTVIEW HERE";
    UIFont *font = [UIFont systemFontOfSize:16.0];
    
    CGSize stringSize = [textToDraw sizeWithFont:font
                               constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
                                   lineBreakMode:UILineBreakModeWordWrap];
    
    CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 350.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);
    
    [textToDraw drawInRect:renderingRect 
                  withFont:font
             lineBreakMode:UILineBreakModeWordWrap
                 alignment:UITextAlignmentLeft];
    return;
    }
    

    我编写这段代码是为了从UIView内容生成pdf。 愿这对你有帮助

    #define kBorderInset            20.0
    #define kBorderWidth            1.0
    #define kMarginInset            10.0
    
    //Line drawing
    #define kLineWidth              1.0
    
        - (IBAction)generatePdfButtonPressed
    {
     pageSize = CGSizeMake(612, 792);
     NSString *fileName = @"profileInfo.pdf";
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
    
     [self generatePdfWithFilePath:pdfFileName];
    }
    
    - (void) generatePdfWithFilePath: (NSString *)thefilePath
    {
     UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
    
     NSInteger currentPage = 0;
     BOOL done = NO;
     do
     {
      // Mark the beginning of a new page.
      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
    
      // Draw a page number at the bottom of each page.
      currentPage++;
      [self drawPageNumber:currentPage];
    
      //Draw a border for each page.
      [self drawBorder];
    
      //Draw text fo our header.
      [self drawHeader];
    
      //Draw a line below the header.
      [self drawLine];
    
      //Draw some text for the page.
      if (profileInfo!=nil) 
      {
       NSString *stringwithInfo=[NSString stringWithFormat:@"Headline: %@\nFirstname: %@\nLastName : %@\nSiteStandardProfileRequest:\n%@",[profileInfo valueForKey:@"headline"],[profileInfo valueForKey:@"firstName"],[profileInfo valueForKey:@"lastName"],[[profileInfo valueForKey:@"siteStandardProfileRequest"]valueForKey:@"url"]];
    
    
       [self drawText:stringwithInfo];
      }
    
      //Draw an image
      [self drawImage];
      done = YES;
     }
     while (!done);
    
     // Close the PDF context and write the contents out.
     UIGraphicsEndPDFContext();
    }
    
    - (void) drawBorder
    {
     CGContextRef    currentContext = UIGraphicsGetCurrentContext();
     UIColor *borderColor = [UIColor brownColor];
     CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2);
     CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor);
     CGContextSetLineWidth(currentContext, kBorderWidth);
     CGContextStrokeRect(currentContext, rectFrame);
    }
    
    
    - (void) drawLine
    {
     CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    
     CGContextSetLineWidth(currentContext, kLineWidth);
    
     CGContextSetStrokeColorWithColor(currentContext, [UIColor blueColor].CGColor);
    
     CGPoint startPoint = CGPointMake(kMarginInset + kBorderInset, kMarginInset + kBorderInset + 40.0);
     CGPoint endPoint = CGPointMake(pageSize.width - 2*kMarginInset -2*kBorderInset, kMarginInset + kBorderInset + 40.0);
    
     CGContextBeginPath(currentContext);
     CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
     CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);
    
     CGContextClosePath(currentContext);
     CGContextDrawPath(currentContext, kCGPathFillStroke);
    }
    
    
    - (void) drawText:(NSString*)txtTodraw
    {
     CGContextRef    currentContext = UIGraphicsGetCurrentContext();
     CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
    
     UIFont *font = [UIFont systemFontOfSize:14.0];
    
     CGSize stringSize = [txtTodraw sizeWithFont:font
                                constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset)
                                    lineBreakMode:UILineBreakModeWordWrap];
    
     CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);
    
     [txtTodraw drawInRect:renderingRect
                   withFont:font
              lineBreakMode:UILineBreakModeWordWrap
                  alignment:UITextAlignmentLeft];
    }
    
    
    - (void) drawImage
    {
     UIImage * demoImage = [UIImage imageNamed:@"demo.png"];
     [demoImage drawInRect:CGRectMake( (pageSize.width - demoImage.size.width/2)/2, 350, demoImage.size.width/2, demoImage.size.height/2)];
    }
    
    
    - (void) drawHeader
    {
     CGContextRef    currentContext = UIGraphicsGetCurrentContext();
     CGContextSetRGBFillColor(currentContext, 0.3, 0.7, 0.2, 1.0);
    
     NSString *textToDraw = @"LinkedIn Profile Info";
    
     UIFont *font = [UIFont systemFontOfSize:24.0];
    
     CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:UILineBreakModeWordWrap];
    
     CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);
    
     [textToDraw drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
    }
    
    
    - (void)drawPageNumber:(NSInteger)pageNumber
    {
     NSString* pageNumberString = [NSString stringWithFormat:@"Page %d", pageNumber];
     UIFont* theFont = [UIFont systemFontOfSize:12];
    
     CGSize pageNumberStringSize = [pageNumberString sizeWithFont:theFont
                                                constrainedToSize:pageSize
                                                    lineBreakMode:UILineBreakModeWordWrap];
    
     CGRect stringRenderingRect = CGRectMake(kBorderInset,
                                             pageSize.height - 40.0,
                                             pageSize.width - 2*kBorderInset,
                                             pageNumberStringSize.height);
    
     [pageNumberString drawInRect:stringRenderingRect withFont:theFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
    }
    
    -(void)fillInformationInform:(NSDictionary*)dics
    {
     headLineLabel.text=[dics valueForKey:@"headline"];
     NSURL *picUrl=[NSURL URLWithString:[dics valueForKey:@"pictureUrl"]];
     profileImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:picUrl]];
    
     phoneNoLabel.text=[[[[dics valueForKey:@"phoneNumbers"] valueForKey:@"values"] objectAtIndex:0] valueForKey:@"phoneNumber"];
    
     cityLabel.text=[[dics valueForKey:@"location"] valueForKey:@"name"];
     emailAddressLabel.text=[dics valueForKey:@"emailAddress"];
     nameLabel.text=[NSString stringWithFormat:@"%@ %@",[dics valueForKey:@"firstName"],[dics valueForKey:@"lastName"]];
    
     dateOfBirthLabel.text=[NSString stringWithFormat:@"%@-%@-%@",[[dics valueForKey:@"dateOfBirth"]valueForKey:@"day"],[[dics valueForKey:@"dateOfBirth"]valueForKey:@"month"],[[dics valueForKey:@"dateOfBirth"]valueForKey:@"year"]];
    
     NSDictionary *eduDic=[[NSDictionary alloc] initWithDictionary:[[[dics valueForKey:@"educations"]valueForKey:@"values"]objectAtIndex:0]];
    
    // NSLog(@"education dic is %@",eduDic);
    
     degreeLabel.text=[eduDic valueForKey:@"degree"];
     durationLabel.text=[NSString stringWithFormat:@"From %@ To %@",[[eduDic valueForKey:@"startDate"]valueForKey:@"year"],[[eduDic valueForKey:@"endDate"]valueForKey:@"year"]];
     fieldOfStudyLabel.text=[eduDic valueForKey:@"fieldOfStudy"];
     collegeLabel.text=[eduDic valueForKey:@"schoolName"];
    
    
     destinationLabel.text=[[[[dics valueForKey:@"positions"] valueForKey:@"values"] objectAtIndex:0] valueForKey:@"title"];
    
     NSArray *skillsArray=[[NSArray alloc] initWithArray:[[dics valueForKey:@"skills"]valueForKey:@"values"]];
    
    
     NSString *skillStr=[[NSString alloc] initWithFormat:@""];
    
     for (NSDictionary *skillDic in skillsArray) 
     {
      if (![skillStr isEqualToString:@""]) {
       skillStr=[skillStr stringByAppendingFormat:@","];
      }
      skillStr=[skillStr stringByAppendingFormat:@"%@",[[skillDic valueForKey:@"skill"]valueForKey:@"name"]];
     }
    
     NSLog(@"skill string is %@",skillStr);
    
     skillsLable.text=[NSString stringWithFormat:@"%@",skillStr];
    
    }
    

    如果您可以在页面上绘制尽可能多的文本,并且在PDF中截取UITextView中的文本时遇到问题,则需要禁用UITextView的滚动。在Interface Builder中或以编程方式:

    textview.scrollEnabled = NO;
    
    当scrollEnabled设置为
    YES
    时,我的PDF中的文本被截断。不用滚动,我就可以得到整个文本——至少是一页所能容纳的文本


    如果您需要在多个页面上生成文本,我认为您必须像其他答案所示,在PDF绘图上下文中单独绘制元素。

    您需要创建PDF drowline和drowRect,然后您可以另存为PDF,您可以得到这一点为什么否决票?试着对新来的人好一点。向上投票以补偿向下投票…感谢斯里卡尔的快速回答。我需要注意的一点是,除了UITextView之外,我的UIView还有其他功能……我有标签、文本字段等,[self.layer renderInContext:pdfContext];以递归方式打印所有子视图,使事情变得简单。话虽如此,我如何才能在UITextView即将呈现时触发此代码…并保持其余子视图不变感谢Hercules。我可能会回到这一点,我希望避免的唯一问题是手动呈现PDF的每个元素。我的视图有图像、标签、文本等。按照这条路线,意味着我需要自己在PDF中重新绘制整个布局,并按照视图中的显示方式选择/定位所有内容…正确吗?
    textview.scrollEnabled = NO;