Created
January 30, 2014 15:35
-
-
Save rsaunders100/8711151 to your computer and use it in GitHub Desktop.
Wrap HTML with a given UIColor and UIFont
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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
Based on this SO answer:
http://stackoverflow.com/a/452883/296446