title: iOS NSAttributedString to HTML
body: <p>I have an <code>NSAttributed</code> string (coming from HTML) that I set for a <code>UITextView</code>. </p>
  
  <pre><code>- (void)setHtml:(NSString *)html {
  
      NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];
  
      // Create the HTML string
      NSDictionary *importParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
      NSError *error = nil;
      self.htmlString = [[NSAttributedString alloc] initWithData:htmlData options:importParams documentAttributes:NULL error:&amp;error];
  
      self.editorView.attributedText = self.htmlString;
  
  }
  </code></pre>
  
  <p>I then let the user edit what they want, and I would like to then convert it out to HTML again, so I use:</p>
  
  <pre><code>- (NSString *)getHTML {
      NSDictionary *exportParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
      NSData *htmlData = [self.editorView.attributedText dataFromRange:NSMakeRange(0, self.editorView.attributedText.length) documentAttributes:exportParams error:nil];
      return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
  }
  </code></pre>
  
  <p>It does return HTML, but it isn't how I want it. Everything is given a class attribute, and the CSS it put at the top of the document. Things like images and links are not even included in the returned HTML and probably tons more issues.</p>
  
  <p><strong>Is there a better way to get HTML from an <code>NSAttributedString</code>? Or, is there a way I could parse the <code>NSAttributedString</code> and write my own HTML?</strong> </p>