I am trying to load an image and add some text to it. my text is in a UILabel and i am trying to add it to a UIImage. I am able to add the text, but i cant seem to get it in the right size. im trying to use the feature label.adjustsFontSizeToFitWidth = YES; but on large images the text comes out very small.
-(UILabel *)xlabel{
if (!xlabel) {
xlabel = [[UILabel alloc] initWithFrame:self.setLablePosition];
xlabel.backgroundColor = [UIColor redColor];
xlabel.adjustsFontSizeToFitWidth = YES;
xlabel.lineBreakMode = UILineBreakModeWordWrap;
xlabel.numberOfLines = 0;
xlabel.textAlignment = UITextAlignmentCenter;
}
return xlabel;
}
-(UIImage *) textToImage:(UIImage *) myImage{
UIImage *watermarkedImage = nil;
UIGraphicsBeginImageContext(appDelegate.theImg.size);
[myImage drawAtPoint: CGPointZero];
[self.xlabel drawTextInRect:CGRectMake(0.0, 0.0, appDelegate.theImg.size.width, appDelegate.theImg.size.height)];
watermarkedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return watermarkedImage;
}
thank you
adjustsFontSizeToFitWidthwill only reduce the size of the font. It will start with font size 17 by default, I believe, and then reduce from there if the text is too long.You can try starting with a larger font size:
and then it will be larger on larger images and reduce as needed for smaller images.
Also,
adjustsFontSizeToFitWidthonly works whennumberOfLinesis set to1.See this: UILabel Documentation