Draw text from uitextview in pdf since it

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(); 
+4
source share
3 answers

I use this to draw my text, it works quite 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]; } 
+4
source

You want to use drawInContext: instead of renderInContext :. renderInContext rasterizes the text, drawInContext writes it as editable, resolution-independent text.

+1
source

Some loss in quality or blurriness is out of scope. The values โ€‹โ€‹for placing the frame on the screen are CGFloat, so you can have a frame located at the point (0.5, 5.7), obviously this makes no sense, and the point .x should be 0 or 1. Decimal values โ€‹โ€‹are confused operating system, therefore it is recommended to ensure that frames always have values โ€‹โ€‹without decimal numbers.

Here you can get more information: http://www.cobiinteractive.com/blog/2011/06/objective-c-blurry-uiview/ or here: http://www.markj.net/iphone-uiview-blurred- blurry-view /

0
source

Source: https://habr.com/ru/post/1392028/


All Articles