Adds HTMLHelper to convert from HTML in an NSString to a properly attributed NSAttributedString.

This commit is contained in:
2020-09-15 20:18:08 -07:00
parent 4ff6a28c67
commit 8029fb7540
3 changed files with 47 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
//
// HtmlHelper.h
// MonsterCards
//
// Created by Tom Hicks on 9/12/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface HTMLHelper : NSObject
+(NSAttributedString*)attributedStringFromHTML:(NSString*)html;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,22 @@
//
// HtmlHelper.m
// MonsterCards
//
// Created by Tom Hicks on 9/12/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import "HTMLHelper.h"
@import UIKit;
@implementation HTMLHelper
+ (NSAttributedString*)attributedStringFromHTML:(NSString *)htmlString {
return [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];
}
@end