将文本从uitextview绘制为pdf [英] Draw text from uitextview to pdf as it is

查看:102
本文介绍了将文本从uitextview绘制为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 4 editable uitextviews, user can set its font font color etc. Now i want to draw pdf of these textviews, but using [self.view.layer renderInContext:UIGraphicsGetCurrentContext()] method causes to lose its quality, so i cannot use this method, instead i am iterating over subviews and drawing the text in pdf. Now my problem is that for some textviews text is printed correctly in pdf,but for other textviews text is not drawn in proper position.This is my code to draw pdf from textview. NSArray *arrayOfsubviews=[self.view subviews];

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();


推荐答案

我用它来绘制我的文字,效果相当不错..也许它可以指向正确的方向。

I use this to draw my text, works fairly well.. maybe it can point you in the right direction.

#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];
}

这篇关于将文本从uitextview绘制为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆