Adds HTMLHelper to convert from HTML in an NSString to a properly attributed NSAttributedString.
This commit is contained in:
19
iOS/MonsterCards/Helpers/HTMLHelper.h
Normal file
19
iOS/MonsterCards/Helpers/HTMLHelper.h
Normal 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
|
||||
22
iOS/MonsterCards/Helpers/HTMLHelper.m
Normal file
22
iOS/MonsterCards/Helpers/HTMLHelper.m
Normal 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
|
||||
Reference in New Issue
Block a user