I am trying to generate a pdf inside my app. To draw a string I'm calculating the size of the bounding rect for that string using boundingRectWithSize and then drawing the string inside a rect of that size.
The code works fine in iOS 7.1 and above but in iOS 7.0.3 the text is not drawn at all if it's width is more than the rectangle's width (400). According to Apple's docs the string should have been wrapped to a new line and clipped if it cannot fit the rect, which is happening in iOS 7.1 and above, but not in iOS 7.0.3.
Here is my code snippet:
-(void)drawText:(NSString *)string
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
myFontForContentBold, NSFontAttributeName,
[NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];
CGRect textRect = [string boundingRectWithSize:CGSizeMake(400, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil]
textRect = CGRectMake(130, 80, 400, textRect.size.height);
[string drawInRect:textRect withAttributes:attrsDictionary];
}
I am not able to figure out what the problem might be. Please help.
Try this:
If you need to draw large chunk of text, consider
Text Kit
.The
Text Kit
approach: