Skip to content

Instantly share code, notes, and snippets.

@mrhether
Created February 24, 2016 19:38
Show Gist options
  • Save mrhether/65a0968e47d691027acf to your computer and use it in GitHub Desktop.
Save mrhether/65a0968e47d691027acf to your computer and use it in GitHub Desktop.
UILabel + HTML
#import "UILabel+HTML.h"
@implementation UILabel (HTML)
- (void) setHtml: (NSString*) html
{
// Add font from label
NSString* finalHtml = [html stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}</style>",
self.font.fontName,
self.font.pointSize]];
NSError *err = nil;
self.attributedText = [[NSAttributedString alloc]
initWithData: [finalHtml dataUsingEncoding:NSUTF8StringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: &err];
if (err) {
NSLog(@"Unable to parse label text: %@", err);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment