I'm using code found here to create an image with text that is scaled to available size:
CGFloat size = 100.0;
CGRect drawRect = CGRectMake(10, 10, 80, 25);
UILabel *myLabel = [[UILabel alloc] initWithFrame:drawRect];
myLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];
myLabel.text = "Hello text!";
myLabel.minimumScaleFactor = 0.5;
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.backgroundColor = [UIColor clearColor];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, 0);
[[myLabel layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[screenshot drawInRect:drawRect];
return screenshot;
This creates a 100x100 image with the rendered label in the top-left corner: (0, 0). How do I get the text at the desired point (10, 10)?
To clarify: I want the label to be the size I specify, be centred horizontally, and its text to scale according to the available size.
Also, what is the purpose of [screenshot drawInRect:drawRect]
because I seem to get the same result without it?