Created
August 7, 2013 11:38
-
-
Save orta/6173320 to your computer and use it in GitHub Desktop.
iOS7 APIs for creating attributed strings from HTML
This file contains hidden or 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
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am able to convert this. May be this help someone else :