Convert HTML to NSAttributed string and vice versa:
Convert HTML to NSAttributed string and vice versa
suggest changeHTML to NSAttributedString:
// HTML String
NSString *htmlString=[[NSString alloc]initWithFormat:@"<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>"];
// Converting HTML string with UTF-8 encoding to NSAttributedString
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil ];
NSAttributedString to HTML:
// ictionary to hold all the attributes of NSAttributed String
NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
// Saving the NSAttributedString with all its attributes as a NSData Entity
NSData *htmlData = [attributedString dataFromRange:NSMakeRange(0, attributedString.length) documentAttributes:documentAttributes error:NULL];
// Convert the NSData into HTML String with UTF-8 Encoding
NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
12UIView
15UIColor
26UIImage
28CALayer
30NSDate
40iBeacon
43UISwitch
49NSTimer
69Fastlane
79NSURL
87AWS SDK
91UIDevice
92CloudKit
96NSData
101Segues
102UIDatePicker
103NSPredicate
104EventKit
105NSBundle
106SiriKit
111StoreKit
112Code signing
1173D Touch
119Keychain
122Block
131Convert HTML to NSAttributed string and vice versa
141AirDrop
144UISlider
145Carthage
146HealthKit
148UI Testing
149Core Motion
151plist
152NSInvocation
155AppDelegate
157MVVM
158UIStoryboard
161MPVolumeView
164UIPhoenix
166Simulator
168NSArray
169OpenGL
175Core Data
179MyLayout
180UIFont
189Security
200Codable
201FileHandle
210Contributors