Skip to content

Instantly share code, notes, and snippets.

@orta
Created August 7, 2013 11:38
Show Gist options
  • Save orta/6173320 to your computer and use it in GitHub Desktop.
Save orta/6173320 to your computer and use it in GitHub Desktop.
iOS7 APIs for creating attributed strings from HTML
//
// NSAttributedString+StringFromHTML.m
// Artsy
//
// Created by Orta on 06/08/2013.
// Copyright (c) 2013 Art.sy. All rights reserved.
//
// This doesn't produce acceptable enough results, and thus is relagated here for another day sometime
#import "NSAttributedString+StringFromHTML.h"
@implementation NSAttributedString (StringFromHTML)
+ (NSAttributedString *)artsyBodyTextAttributedStringFromHTML:(NSString *)HTML
{
UIFont *font = [ARTheme defaultTheme].fonts[@"PostBody"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineHeightMultiple = 1.2;
style.paragraphSpacing = font.pointSize * .5;
NSDictionary *textParams = @{
NSFontAttributeName : font,
NSParagraphStyleAttributeName : style,
NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)
};
return [self _attributedStringWithTextParams:textParams andHTML:HTML];
}
+ (NSAttributedString *)artsyPostTitleTextAttributedStringFromHTML:(NSString *)HTML
{
UIFont *font = [ARTheme defaultTheme].fonts[@"PostTitle"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineHeightMultiple = 1.1;
style.paragraphSpacing = font.pointSize * .75;
NSDictionary *textParams = @{
NSFontAttributeName : font,
NSForegroundColorAttributeName : [UIColor orangeColor]
};
return [self _attributedStringWithTextParams:textParams andHTML:HTML];
}
/// As we can't use the attributed string attributes params, so generate CSS from it
+ (NSString *)_cssStringFromAttributedStringAttributes:(NSDictionary *)dictionary
{
NSMutableString *cssString = [NSMutableString stringWithString:@"<style> p {"];
if ([dictionary objectForKey:NSFontAttributeName]) {
UIFont *font = dictionary[NSFontAttributeName];
[cssString appendFormat:@"font-family:'%@'; font-size: %0.1fpx;", font.fontName, roundf(font.pointSize)];
}
if (dictionary[NSParagraphStyleAttributeName]) {
NSParagraphStyle *style = dictionary[NSParagraphStyleAttributeName];
[cssString appendFormat:@"line-height:%f em;", style.lineHeightMultiple];
}
[cssString appendString:@"}"];
[cssString appendString:@"</style><body>"];
return cssString;
}
+ (NSAttributedString *)_attributedStringWithTextParams:(NSDictionary *)textParams andHTML:(NSString *)HTML
{
NSDictionary *importParams = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
NSError *error = nil;
NSString *formatString = [[self _cssStringFromAttributedStringAttributes:textParams] stringByAppendingFormat:@"%@</body>", HTML];
NSData *stringData = [formatString dataUsingEncoding:NSUnicodeStringEncoding] ;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:stringData options:importParams documentAttributes:NULL error:&error];
if (error) {
ARErrorLog(@"Error creating NSAttributedString from HTML %@", error.localizedDescription);
return nil;
}
return attributedString;
}
@end
@ehlersd
Copy link

ehlersd commented Jun 18, 2014

I would like to request permission to use a derived version of this basic function in my DoubleNodePod library.

@orta
Copy link
Author

orta commented Aug 12, 2014

sure thing

@muzammil-triffort
Copy link

How to convert NSAttributedString in HTML string?

@muzammil-triffort
Copy link

I am able to convert this. May be this help someone else :

NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, nil];

NSData *htmlData = [string dataFromRange:NSMakeRange(0, string.length) documentAttributes:documentAttributes error:NULL];

NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment