Use a UIImageView to display your image and add a UILabel as the subview of UIImageView.
1.configure your UILabel first:
[yourLabel setNumberOfLines:0];
[yourLabel setLineBreakMode:NSLineBreakByWordWrapping];
2.get your label frame with the contents length
- (CGRect)labelFrameWithText:(NSString *)text
{
CGRect rect;
// the font of your text
UIFont *font = [UIFont systemFontOfSize:15.0];
NSDictionary *attributes = @{NSFontAttributeName: font};
// the first parametric CGSize is the max size that the rect's size can be
CGRect boundingRect = [title boundingRectWithSize:CGSizeMake(youImageWidth, 100.0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
//the rect of the UILabel
//This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
rect = CGRectMake(yourLabelOriginX,
yourLabelOriginY,
ceil(boundingRect.size.width),
ceil(boundingRect.size.height));
return rect;
}
3.change your label's frame and setText
------- old version ---------
CGSize contentSize = [content sizeWithFont:font
constrainedToSize:CGSizeMake(maxWidth, maxHeight)
lineBreakMode: NSLineBreakByWordWrapping];