iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – 将文本从uitextview绘制为pdf大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有4个可编辑的uitextviews,用户可以设置其字体颜色等.现在我想绘制这些textview的pdf,但使用[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]方法会导致其质量下降,所以我无法使用这个方法,而不是迭代子视图和绘制PDF格式的文本.现在我的问题是,对于某些文本视图,文本在pdf中正确打印,但对于其他文本视图,文本不是在正确的位置绘制的.这是我从textview中绘制pdf的代码.
         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,916); 
        CGContextScaleCTM(context,1.0,-1.0);


        CTFrameDraw(frameRef,context);


        CGContextRestoreGState(context);
        CGPathRelease(pathRef);


    }
   }
UIGraphicsEndPDFContext();

解决方法

用这个来绘制我的文字,效果相当好……也许它可以指出你正确的方向.

#define kBorderInset 20.0
#define kMarginInset 10.0

- (void) drawText{
CGContextRef    currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext,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) 
                               lineBrea@R_489_11076@e:UILineBrea@R_489_11076@eWordWrap];

CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset,kBorderInset + kMarginInset + 350.0,pageSize.width - 2*kBorderInset - 2*kMarginInset,StringSize.height);

[textToDraw drawInRect:renderingRect 
              withFont:font
         lineBrea@R_489_11076@e:UILineBrea@R_489_11076@eWordWrap
             alignment:UITextAlignmentLeft];
}

大佬总结

以上是大佬教程为你收集整理的objective-c – 将文本从uitextview绘制为pdf全部内容,希望文章能够帮你解决objective-c – 将文本从uitextview绘制为pdf所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。