【问题标题】:Draw text from uitextview to pdf as it is按原样将文本从 uitextview 绘制为 pdf
【发布时间】:2012-02-14 23:28:25
【问题描述】:

我有 4 个可编辑的 uitextviews,用户可以设置其字体字体颜色等。现在我想绘制这些 textviews 的 pdf,但是使用 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()] 方法会导致其质量下降,所以我不能使用这种方法,而是迭代子视图并在 pdf 中绘制文本。现在我的问题是,对于某些 textviews 文本在 pdf 中正确打印,但对于其他 textviews 文本没有绘制在正确的位置。这是我从 textview 绘制 pdf 的代码。 NSArray *arrayOfsubviews=[self.view 子视图];

for (int i=0; i<[arrayOfsubviews count]; i++) {

    if ([[arrayOfsubviews objectAtIndex:i]isKindOfClass:[UITextView class]]) {

        UITextView *texts=[arrayOfsubviews objectAtIndex:i];


        CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)texts.font.fontName, texts.font.pointSize,NULL);
        CGColorRef color = texts.textColor.CGColor;

        NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                        (__bridge id)ctFont, (id)kCTFontAttributeName,
                                        color, (id)kCTForegroundColorAttributeName,nil];


        NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:texts.text
                                                                           attributes:attributesDict];
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) stringToDraw);



        CGRect rect=CGRectMake(texts.frame.origin.x,916-texts.frame.origin.y-texts.frame.size.height, texts.frame.size.width, texts.frame.size.height);


        CGMutablePathRef pathRef = CGPathCreateMutable();
        CGPathAddRect(pathRef, NULL,rect);
        CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0),pathRef, NULL);


        CGContextRef context = UIGraphicsGetCurrentContext();


        CGContextSaveGState(context);

        CGContextTranslateCTM(context, 0,916); 
        CGContextScaleCTM(context, 1.0, -1.0);


        CTFrameDraw(frameRef, context);


        CGContextRestoreGState(context);
        CGPathRelease(pathRef);


    }
   }
UIGraphicsEndPDFContext();

【问题讨论】:

    标签: objective-c ios ipad pdf uitextview


    【解决方案1】:

    一些质量下降或视图模糊是因为框架。在屏幕中定位帧的值是 CGFloat,所以你可以有一个帧位于点 (0.5, 5.7),显然这没有任何意义,并且 point.x 必须是 0 或 1。十进制值混淆操作系统,因此建议确保帧始终具有不带十进制数字的值。

    您可以在此处获取更多信息:http://www.cobiinteractive.com/blog/2011/06/objective-c-blurry-uiview/ 或此处:http://www.markj.net/iphone-uiview-blurred-blurry-view/

    【讨论】:

      【解决方案2】:

      您想使用 drawInContext: 而不是 renderInContext:。 renderInContext 将对文本进行光栅化,drawInContext 将其编码为可编辑、与分辨率无关的文本。

      【讨论】:

        【解决方案3】:

        我用它来画我的文字,效果很好。也许它可以为你指明正确的方向。

        #define kBorderInset 20.0
        #define kMarginInset 10.0
        
        - (void) drawText{
        CGContextRef    currentContext = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
        
        NSString *textToDraw = @"Your Text Here";
        UIFont *font = [UIFont systemFontOfSize:14.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];
        }
        

        【讨论】:

          猜你喜欢
          • 2011-10-28
          • 2014-05-25
          • 1970-01-01
          • 1970-01-01
          • 2014-07-04
          • 2011-01-09
          • 1970-01-01
          • 1970-01-01
          • 2013-05-27
          相关资源
          最近更新 更多