Skip to content

Instantly share code, notes, and snippets.

@miketihonchik
Created August 29, 2013 16:39
Show Gist options
  • Save miketihonchik/6380472 to your computer and use it in GitHub Desktop.
Save miketihonchik/6380472 to your computer and use it in GitHub Desktop.
draw text over any image (pass additional parameters, so that text is not pixelated)
- (UIImage *)drawTextOnImage:(NSString *)text :(UIImage *)img {
UIGraphicsBeginImageContextWithOptions(img.size, NO, [UIScreen mainScreen].scale);
CGRect imageBoundaries = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:imageBoundaries];
imageBoundaries.origin.y = (imageBoundaries.size.height - 14) / 2 - 4;
imageBoundaries = CGRectIntegral(imageBoundaries);
imageBoundaries.size.height = 14;
[[UIColor grayColor] set];
[text drawInRect:imageBoundaries withFont:[UIFont boldSystemFontOfSize:14] lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
@markjwigham
Copy link

I am using this, but whatever I do, the end image is blurred.
Have you every had a similar experience?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment