Skip to content
This repository has been archived by the owner on Aug 19, 2019. It is now read-only.

Added Support for additional tags #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HTMLToAttributedString.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -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; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -82,6 +85,7 @@
AA9A4B7F166FE7A80006F4EE /* Frameworks */ = {
isa = PBXGroup;
children = (
7D12230A20048D4600E39A2C /* CoreText.framework */,
AA9A4B80166FE7A80006F4EE /* UIKit.framework */,
AA9A4B82166FE7A80006F4EE /* Foundation.framework */,
AA9A4B84166FE7A80006F4EE /* CoreGraphics.framework */,
Expand Down
6 changes: 3 additions & 3 deletions HTMLToAttributedString/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ - (void)viewDidLoad
// Do any additional setup after loading the view, typically from a nib.


NSAttributedString *attrString = [NSAttributedString attributedStringFromHTML:@"<font face=\"Avenir-Heavy\" color=\"#FF0000\">This</font> <shadow>is</shadow> <b>Happy bold, <u>underlined</u>, <stroke width=\"2.0\" color=\"#00FF00\">awesomeness </stroke><a href=\"https://www.google.com\">link</a>!</b> <br/> <i>And some italic on the next line.</i><img src=\"car.png\" width=\"50\" height=\"50\" />"
normalFont:[UIFont systemFontOfSize:12]
boldFont:[UIFont boldSystemFontOfSize:12]
NSAttributedString *attrString = [NSAttributedString attributedStringFromHTML:@"<h1>Heading1</h1><br/><h3>Heading2</h3><br/><h5>Heading3</h5><br/><sup>superscript</sup>normal<sub>subscript</sub><br/><font face=\"Avenir-Heavy\" color=\"#FF0000\">This</font> <shadow>is</shadow> <b>Happy bold, <u>underlined</u>, <stroke width=\"2.0\" color=\"#00FF00\">awesomeness </stroke><a href=\"https://www.google.com\">link</a>!</b> <br/> <i>And some italic on the next line.</i><img src=\"car.png\" width=\"50\" height=\"50\" /> <br/> <br/> <samp>printf(\"Hello World!\")</samp><br/>"
normalFont:[UIFont systemFontOfSize:12.0]
boldFont:[UIFont boldSystemFontOfSize:12.0]
italicFont:[UIFont italicSystemFontOfSize:12.0]
];
self.label.attributedText = attrString;
Expand Down
15 changes: 14 additions & 1 deletion NSAttributedString+DDHTML/NSAttributedString+DDHTML.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \<b\> and \<strong\> tags
* @param italicFont Font to use for \<i\> and \<em\> tags
* @param fixedFont Font to use for \<code\> and \<samp\> 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.
*
Expand All @@ -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<NSString *, UIImage *> *)imageMap;
+ (NSAttributedString *)attributedStringFromHTML:(NSString *)htmlString normalFont:(UIFont *)normalFont boldFont:(UIFont *)boldFont italicFont:(UIFont *)italicFont fixedFont:(UIFont *)fixedFont imageMap:(NSDictionary<NSString *, UIImage *> *)imageMap;

@end

Expand Down
76 changes: 70 additions & 6 deletions NSAttributedString+DDHTML/NSAttributedString+DDHTML.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
//

#import "NSAttributedString+DDHTML.h"
#import <CoreText/CTStringAttributes.h>
#include <libxml/HTMLparser.h>

@implementation NSAttributedString (DDHTML)
Expand All @@ -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
Expand All @@ -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<NSString *, UIImage *> *)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<NSString *, UIImage *> *)imageMap
{
// Parse HTML string as XML document using UTF-8 encoding
NSData *documentData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
Expand All @@ -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;
Expand All @@ -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<NSString *, UIImage *> *)imageMap
+ (NSAttributedString *)attributedStringFromNode:(xmlNodePtr)xmlNode
normalFont:(UIFont *)normalFont
boldFont:(UIFont *)boldFont
italicFont:(UIFont *)italicFont
fixedFont:(UIFont *)fixedFont
imageMap:(NSDictionary<NSString *, UIImage *> *)imageMap
{
NSMutableAttributedString *nodeAttributedString = [[NSMutableAttributedString alloc] init];

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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];
}

Expand Down