I have 4 editable uitextviews, user can set font font color, etc. Now I want to draw a pdf of these text views, but using the [self.view.layer renderInContext: UIGraphicsGetCurrentContext ()] method leads to a loss of its quality, so I can not use this method, I iterate over the subviews instead and draw the text in pdf . Now my problem is that for some text texts the text prints correctly in pdf, but for other texts the text is not drawn in the correct position. This is my code for drawing 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();
source share