Skip to content

@rsaunders100 /gist:8711151
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.