Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created January 30, 2014 15:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsaunders100/8711151 to your computer and use it in GitHub Desktop.
Save rsaunders100/8711151 to your computer and use it in GitHub Desktop.
Wrap HTML with a given UIColor and UIFont
+ (NSString *)htmlFromBodyString:(NSString *)htmlBodyString
textFont:(UIFont *)font
textColor:(UIColor *)textColor
{
int numComponents = CGColorGetNumberOfComponents([textColor CGColor]);
NSAssert(numComponents == 4 || numComponents == 2, @"Unsupported color format");
// E.g. FF00A5
NSString *colorHexString = nil;
const CGFloat *components = CGColorGetComponents([textColor CGColor]);
if (numComponents == 4)
{
unsigned int red = components[0] * 255;
unsigned int green = components[1] * 255;
unsigned int blue = components[2] * 255;
colorHexString = [NSString stringWithFormat:@"%02X%02X%02X", red, green, blue];
}
else
{
unsigned int white = components[0] * 255;
colorHexString = [NSString stringWithFormat:@"%02X%02X%02X", white, white, white];
}
NSString *HTML = [NSString stringWithFormat:@"<html>\n"
"<head>\n"
"<style type=\"text/css\">\n"
"body {font-family: \"%@\"; font-size: %@; color:#%@;}\n"
"</style>\n"
"</head>\n"
"<body>%@</body>\n"
"</html>",
font.familyName, @(font.pointSize), colorHexString, htmlBodyString];
return HTML;
}
@rsaunders100
Copy link
Author

Based on this SO answer:
http://stackoverflow.com/a/452883/296446

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