public
Created

Wrap HTML with a given UIColor and UIFont

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
+ (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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.