Dvir Levy
Dvir Levy

Reputation: 8118

UILabel on image

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

Upvotes: 0

Views: 480

Answers (1)

Justin Paulson
Justin Paulson

Reputation: 4388

adjustsFontSizeToFitWidth will 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:

xlabel.font = [UIFont systemFontOfSize:25];

and then it will be larger on larger images and reduce as needed for smaller images.

Also, adjustsFontSizeToFitWidth only works when numberOfLines is set to 1.

See this: UILabel Documentation

Upvotes: 2

Related Questions