NSString drawAtPoint with

2019-08-20 06:04发布

问题:

I want to make some UIImage with label, which will show, if photo isn't loaded. Generation of this image in application is more useful, because it very easy to localize text. I have code:

NSString *text = @"Photo not loaded yet. Please wait.";
UIFont *font = [UIFont boldSystemFontOfSize:35];
CGSize imageSize = CGSizeMake(400, 400);

CGSize constraint = CGSizeMake(imageSize.width * 0.8f, imageSize.height * 0.8f);
CGSize textSize = [text sizeWithFont:font constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);

CGPoint pointToDraw = CGPointMake((imageSize.width - textSize.width) / 2, (imageSize.height - textSize.height) / 2);

[text drawAtPoint:pointToDraw forWidth:textSize.width withFont:font lineBreakMode:NSLineBreakByWordWrapping];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;

But it draw only first line, like in screenshot.

Where I was wrong?

Thank you.

回答1:

From the documentation for -drawAtPoint:forWidth:withFont::

Draws the string in a single line at the specified point in the current graphics context using the specified font and attributes.

Ergo, use a different method. -drawInRect:withFont:lineBreakMode: looks like a solid candidate.



标签: ios uiimage