diff --git a/HTMLToAttributedString.xcodeproj/project.pbxproj b/HTMLToAttributedString.xcodeproj/project.pbxproj index 43cec2c..aa55726 100644 --- a/HTMLToAttributedString.xcodeproj/project.pbxproj +++ b/HTMLToAttributedString.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 7D12230B20048D4600E39A2C /* CoreText.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7D12230A20048D4600E39A2C /* CoreText.framework */; }; AA9A4B81166FE7A80006F4EE /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA9A4B80166FE7A80006F4EE /* UIKit.framework */; }; AA9A4B83166FE7A80006F4EE /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA9A4B82166FE7A80006F4EE /* Foundation.framework */; }; AA9A4B85166FE7A80006F4EE /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA9A4B84166FE7A80006F4EE /* CoreGraphics.framework */; }; @@ -24,6 +25,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 7D12230A20048D4600E39A2C /* CoreText.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreText.framework; path = System/Library/Frameworks/CoreText.framework; sourceTree = SDKROOT; }; AA9A4B7C166FE7A80006F4EE /* HTMLToAttributedString.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HTMLToAttributedString.app; sourceTree = BUILT_PRODUCTS_DIR; }; AA9A4B80166FE7A80006F4EE /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; AA9A4B82166FE7A80006F4EE /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -51,6 +53,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 7D12230B20048D4600E39A2C /* CoreText.framework in Frameworks */, AA9A4BA7166FF1AA0006F4EE /* libxml2.dylib in Frameworks */, AA9A4B81166FE7A80006F4EE /* UIKit.framework in Frameworks */, AA9A4B83166FE7A80006F4EE /* Foundation.framework in Frameworks */, @@ -82,6 +85,7 @@ AA9A4B7F166FE7A80006F4EE /* Frameworks */ = { isa = PBXGroup; children = ( + 7D12230A20048D4600E39A2C /* CoreText.framework */, AA9A4B80166FE7A80006F4EE /* UIKit.framework */, AA9A4B82166FE7A80006F4EE /* Foundation.framework */, AA9A4B84166FE7A80006F4EE /* CoreGraphics.framework */, diff --git a/HTMLToAttributedString/ViewController.m b/HTMLToAttributedString/ViewController.m index 73d6171..d90980c 100644 --- a/HTMLToAttributedString/ViewController.m +++ b/HTMLToAttributedString/ViewController.m @@ -23,9 +23,9 @@ - (void)viewDidLoad // Do any additional setup after loading the view, typically from a nib. - NSAttributedString *attrString = [NSAttributedString attributedStringFromHTML:@"This is Happy bold, underlined, awesomeness link!
And some italic on the next line." - normalFont:[UIFont systemFontOfSize:12] - boldFont:[UIFont boldSystemFontOfSize:12] + NSAttributedString *attrString = [NSAttributedString attributedStringFromHTML:@"

Heading1


Heading2


Heading3

superscriptnormalsubscript
This is Happy bold, underlined, awesomeness link!
And some italic on the next line.

printf(\"Hello World!\")
" + normalFont:[UIFont systemFontOfSize:12.0] + boldFont:[UIFont boldSystemFontOfSize:12.0] italicFont:[UIFont italicSystemFontOfSize:12.0] ]; self.label.attributedText = attrString; diff --git a/NSAttributedString+DDHTML/NSAttributedString+DDHTML.h b/NSAttributedString+DDHTML/NSAttributedString+DDHTML.h index 911d0ce..f79d970 100755 --- a/NSAttributedString+DDHTML/NSAttributedString+DDHTML.h +++ b/NSAttributedString+DDHTML/NSAttributedString+DDHTML.h @@ -67,6 +67,19 @@ NS_ASSUME_NONNULL_BEGIN */ + (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont; +/** + * Generates an attributed string from HTML. + * + * @param htmlString HTML String + * @param normalFont Font to use for general text + * @param boldFont Font to use for \ and \ tags + * @param italicFont Font to use for \ and \ tags + * @param fixedFont Font to use for \ and \ tags + * + * @return Attributed string + */ ++ (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont fixedFont:(UIFont *)fixedFont; + /** * Generates an attributed string from HTML. * @@ -78,7 +91,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return Attributed string */ -+ (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont imageMap:(NSDictionary *)imageMap; ++ (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont fixedFont:(UIFont *)fixedFont imageMap:(NSDictionary *)imageMap; @end diff --git a/NSAttributedString+DDHTML/NSAttributedString+DDHTML.m b/NSAttributedString+DDHTML/NSAttributedString+DDHTML.m index 24f5d19..947ec38 100755 --- a/NSAttributedString+DDHTML/NSAttributedString+DDHTML.m +++ b/NSAttributedString+DDHTML/NSAttributedString+DDHTML.m @@ -29,6 +29,7 @@ // #import "NSAttributedString+DDHTML.h" +#import #include @implementation NSAttributedString (DDHTML) @@ -40,7 +41,8 @@ + (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString return [self attributedStringFromHTML:htmlString normalFont:preferredBodyFont boldFont:[UIFont boldSystemFontOfSize:preferredBodyFont.pointSize] - italicFont:[UIFont italicSystemFontOfSize:preferredBodyFont.pointSize]]; + italicFont:[UIFont italicSystemFontOfSize:preferredBodyFont.pointSize] + fixedFont:[UIFont fontWithName:@"Menlo-Regular" size:preferredBodyFont.pointSize]]; } + (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont @@ -56,10 +58,22 @@ + (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFo normalFont:normalFont boldFont:boldFont italicFont:italicFont + fixedFont:[UIFont fontWithName:@"Menlo-Regular" size:normalFont.pointSize] imageMap:@{}]; } -+ (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont imageMap:(NSDictionary *)imageMap ++ (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont fixedFont:(UIFont *)fixedFont +{ + return [self attributedStringFromHTML:htmlString + normalFont:normalFont + boldFont:boldFont + italicFont:italicFont + fixedFont:fixedFont + imageMap:@{}]; +} + + ++ (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont fixedFont:(UIFont *)fixedFont imageMap:(NSDictionary *)imageMap { // Parse HTML string as XML document using UTF-8 encoding NSData *documentData = [htmlString dataUsingEncoding:NSUTF8StringEncoding]; @@ -73,7 +87,7 @@ + (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFo xmlNodePtr currentNode = document->children; while (currentNode != NULL) { - NSAttributedString *childString = [self attributedStringFromNode:currentNode normalFont:normalFont boldFont:boldFont italicFont:italicFont imageMap:imageMap]; + NSAttributedString *childString = [self attributedStringFromNode:currentNode normalFont:normalFont boldFont:boldFont italicFont:italicFont fixedFont:fixedFont imageMap:imageMap]; [finalAttributedString appendAttributedString:childString]; currentNode = currentNode->next; @@ -84,7 +98,12 @@ + (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFo return finalAttributedString; } -+ (NSAttributedString *)attributedStringFromNode:(xmlNodePtr)xmlNode normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont imageMap:(NSDictionary *)imageMap ++ (NSAttributedString *)attributedStringFromNode:(xmlNodePtr)xmlNode + normalFont:(UIFont *)normalFont + boldFont:(UIFont *)boldFont + italicFont:(UIFont *)italicFont + fixedFont:(UIFont *)fixedFont + imageMap:(NSDictionary *)imageMap { NSMutableAttributedString *nodeAttributedString = [[NSMutableAttributedString alloc] init]; @@ -96,7 +115,7 @@ + (NSAttributedString *)attributedStringFromNode:(xmlNodePtr)xmlNode normalFont: // Handle children xmlNodePtr currentNode = xmlNode->children; while (currentNode != NULL) { - NSAttributedString *childString = [self attributedStringFromNode:currentNode normalFont:normalFont boldFont:boldFont italicFont:italicFont imageMap:imageMap]; + NSAttributedString *childString = [self attributedStringFromNode:currentNode normalFont:normalFont boldFont:boldFont italicFont:italicFont fixedFont:fixedFont imageMap:imageMap]; [nodeAttributedString appendAttributedString:childString]; currentNode = currentNode->next; @@ -124,6 +143,49 @@ + (NSAttributedString *)attributedStringFromNode:(xmlNodePtr)xmlNode normalFont: } } + // Mark tag + else if (strncmp("mark", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + [nodeAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:nodeAttributedStringRange]; + } + + // h5 tag + else if (strncmp("h5", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + UIFont *headingFont = [boldFont fontWithSize:(boldFont.pointSize+3.0)]; + [nodeAttributedString addAttribute:NSFontAttributeName value:headingFont range:nodeAttributedStringRange]; + } + + // h3 tag + else if (strncmp("h3", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + UIFont *headingFont = [boldFont fontWithSize:(boldFont.pointSize+6.0)]; + [nodeAttributedString addAttribute:NSFontAttributeName value:headingFont range:nodeAttributedStringRange]; + } + // h1 tag + else if (strncmp("h1", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + UIFont *headingFont = [boldFont fontWithSize:(boldFont.pointSize+9.0)]; + [nodeAttributedString addAttribute:NSFontAttributeName value:headingFont range:nodeAttributedStringRange]; + } + + // Superscript tag + else if (strncmp("sup", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + [nodeAttributedString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:nodeAttributedStringRange]; + } + + // Subscript tag + else if (strncmp("sub", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + [nodeAttributedString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"-1" range:nodeAttributedStringRange]; + } + + + // Code/Samp tags + if (strncmp("code", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0 || + strncmp("samp", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + if (fixedFont) { + UIColor *backgroundColorRef = [UIColor colorWithRed:236.0f/255.0f green:222.0f/255.0f blue:180.0f/255.0f alpha:1.0f]; + [nodeAttributedString addAttribute:NSFontAttributeName value:fixedFont range:nodeAttributedStringRange]; + [nodeAttributedString addAttribute:NSBackgroundColorAttributeName value:backgroundColorRef range:nodeAttributedStringRange]; + } + } + // Bold Tag if (strncmp("b", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0 || strncmp("strong", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { @@ -146,7 +208,9 @@ + (NSAttributedString *)attributedStringFromNode:(xmlNodePtr)xmlNode normalFont: } // Stike Tag - else if (strncmp("strike", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { + else if (strncmp("strike", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0 || + strncmp("del", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0 || + strncmp("s", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) { [nodeAttributedString addAttribute:NSStrikethroughStyleAttributeName value:@(YES) range:nodeAttributedStringRange]; }