Skip to content

Instantly share code, notes, and snippets.

@danielnorton
Last active August 9, 2016 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielnorton/7027293 to your computer and use it in GitHub Desktop.
Save danielnorton/7027293 to your computer and use it in GitHub Desktop.
Unexpected roundtrip of HTML through NSAttributedString. This demonstrates a simple HTML file going into an NSAttributedString and then writing that string back out to a new HTML file. The output file is structured very differently than the input file. This was discovered while experimenting with using a UITextView as a rich text editor.
- (NSAttributedString *)loadContent {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"input" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *options = @{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
};
NSAttributedString *attributed = [[NSAttributedString alloc] initWithData:htmlData
options:options
documentAttributes:nil
error:nil];
return attributed;
}
- (void)showContent:(NSAttributedString *)string {
NSRange range = NSMakeRange(0, string.length);
NSError *error = nil;
NSData *data = [string dataFromRange:range
documentAttributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
error:&error];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"output.html"];
[data writeToFile:file atomically:YES];
}
<h1>Julio-Claudian Emperors</h1>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Augustus" target="_new">Augustus<a></li>
<li>Tiberius</li>
<li>Caligula</li>
<li>Claudius</li>
<li>Nero</li>
</ul>
<hr/>
<h1>Year of the Four Emperors</h1>
<ol>
<li><em>Galba</em></li>
<li style="color:white;background-color:blue;">Otho</li>
<li><b>Vitellius</b></li>
<li><span style="font-style:italic;">Vespasian</span></li>
</ol>
<img alt="SPQR" src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Roman_SPQR_banner.svg/200px-Roman_SPQR_banner.svg.png" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000}
p.p4 {margin: 0.0px 0.0px 6.0px 0.0px; font: 12.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000; min-height: 13.8px}
li.li2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Times New Roman'; color: #0000ee; -webkit-text-stroke: #0000ee}
li.li3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000}
li.li5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Times New Roman'; color: #ffffff; -webkit-text-stroke: #ffffff; background-color: #0000ff}
span.s1 {font-family: 'TimesNewRomanPS-BoldMT'; font-weight: bold; font-style: normal; font-size: 24.00pt; font-kerning: none}
span.s2 {font-family: 'Times New Roman'; font-weight: normal; font-style: normal; font-size: 12.00pt; -webkit-text-stroke: 0px #000000}
span.s3 {font-family: 'Times New Roman'; font-weight: normal; font-style: normal; font-size: 12.00pt; text-decoration: underline ; font-kerning: none}
span.s4 {font-family: 'Times New Roman'; font-weight: normal; font-style: normal; font-size: 12.00pt; font-kerning: none}
span.s5 {font-family: 'TimesNewRomanPS-ItalicMT'; font-weight: normal; font-style: italic; font-size: 12.00pt; -webkit-text-stroke: 0px #000000}
span.s6 {font-family: 'TimesNewRomanPS-ItalicMT'; font-weight: normal; font-style: italic; font-size: 12.00pt; font-kerning: none}
span.s7 {font-family: 'TimesNewRomanPS-BoldMT'; font-weight: bold; font-style: normal; font-size: 12.00pt; -webkit-text-stroke: 0px #000000}
span.s8 {font-family: 'TimesNewRomanPS-BoldMT'; font-weight: bold; font-style: normal; font-size: 12.00pt; font-kerning: none}
ol.ol1 {list-style-type: decimal}
ul.ul1 {list-style-type: disc}
</style>
</head>
<body>
<h1 style="margin: 0.0px 0.0px 16.1px 0.0px; font: 24.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000"><span class="s1">Julio-Claudian Emperors</span></h1>
<ul class="ul1">
<li class="li2"><span class="s2"><a href="http://en.wikipedia.org/wiki/Augustus"><span class="s3">Augustus</span></a></span></li>
<li class="li3"><span class="s2"></span><span class="s4">Tiberius</span></li>
<li class="li3"><span class="s2"></span><span class="s4">Caligula</span></li>
<li class="li3"><span class="s2"></span><span class="s4">Claudius</span></li>
<li class="li3"><span class="s2"></span><span class="s4">Nero</span></li>
</ul>
<p class="p4"><span class="s4"></span><br></p>
<h1 style="margin: 0.0px 0.0px 16.1px 0.0px; font: 24.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000"><span class="s1">Year of the Four Emperors</span></h1>
<ol class="ol1">
<li class="li3"><span class="s5"></span><span class="s6">Galba</span></li>
<li class="li5"><span class="s2"></span><span class="s4">Otho</span></li>
<li class="li3"><span class="s7"></span><span class="s8">Vitellius</span></li>
<li class="li3"><span class="s5"></span><span class="s6">Vespasian</span></li>
</ol>
<p class="p3"><span class="s4"></span></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment