The idea is to split the text into blocks and single lines and to use for each part
- (void)drawInRect:(CGRect)rect;
or
void CTLineDraw(CTLineRef line, CGContextRef context);
Solution:
@import CoreText;
<...>
- (IBAction)createPDF:(id)sender {
PDFFileName = <yourString>;
PDFPath = <yourPath>;
[self generatePdfWithFilePath:PDFPath];
}
- (void)generatePdfWithFilePath:(NSString *)thefilePath {
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
BOOL done = NO;
do {
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 712), nil);
[self drawText];
done = YES;
}
while (!done);
UIGraphicsEndPDFContext();
}
- (void)drawText {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//Here you can set up attributes for your text
NSDictionary *attributesForText = @{
NSFontAttributeName:[UIFont fontWithName:@"Georgia" size:12]
};
//Drawing text in line
NSMutableAttributedString *stringOneAttributed = [[NSMutableAttributedString alloc] initWithString:stringOne attributes:attributesForText];
CTLineRef stringOneLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)(stringOneAttributed));
CGContextSetTextPosition(currentContext, x, y);
CTLineDraw(stringOneLine, currentContext);
CFRelease(stringOneLine);
//Drawing block of text in rectangle
NSMutableAttributedString *stringTwoAttributed = [[NSMutableAttributedString alloc] initWithString:stringTwo attributes:attributesForText];
[stringTwoAttributed drawInRect:CGRectMake(x, y, width, height)];
}