title: How to maintain indentations of HTML content in UITextView in Swift?
body: <p>I'm showing a lists &amp; sublists in <code>UITextView</code> but it doesn't show indentation for sublists. Here is my code, getting data from HTML format here.</p>
  
  <pre><code>extension NSAttributedString {
  convenience public init?(styled: String, textAlignment: NSTextAlignment = .center, color: UIColor = .black) {
      guard let data = styled.data(using: String.Encoding.unicode) else {
          return nil
      }
      do {
          let string = try NSMutableAttributedString(data: data,
                                                     options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
          let paragraphStyle = NSMutableParagraphStyle()
  
          paragraphStyle.tabStops = [NSTextTab(textAlignment: .natural, location: 0, options: Dictionary&lt;NSTextTab.OptionKey, Any&gt;())]
          paragraphStyle.defaultTabInterval = 5
  
          string.addAttributes(
              [NSAttributedStringKey.foregroundColor: color,NSAttributedStringKey.paragraphStyle:paragraphStyle],
              range: NSMakeRange(0, string.length)
          )
  
          self.init(attributedString: string.removingTrailingNewLine)
      } catch {
          return nil
      }
  }
  </code></pre>
  
  <p>HTML data to be shown, although it contains ul, ol tags but adding CSS for those tags doesn't apply inside <code>UITextView</code>.</p>
  
  <pre><code>    &lt;html&gt;
  
      &lt;head&gt;
      &lt;style type="text/css"&gt;
      body {
        font-size: 14px;
        font-family: -apple-system, Arial, sans-serif;
        color: black;
        margin-bottom: 0px;
        padding-bottom: 0px;
        line-height: 20px;
      }
  
      ul,
      ol {
        padding-left: 10px;
      }
      &lt;/style&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
      &lt;p&gt;Professionals are often vastly more comfortable solving analytical, convergent problems than those requiring more divergent thinking. In this article, Olivier Leclerc and Mihnea Moldoveanu share some strategies for stimulating truly novel thinking.
      They introduce five "flexons," which can be thought of as "languages" for shaping problems that help you bring diverse perspectives to problem solving:&lt;/p&gt;
  
    &lt;ul type="disc"&gt;
      &lt;li&gt;Networks&lt;/li&gt;
      &lt;li&gt;Evolutionary&lt;/li&gt;
    &lt;/ul&gt;
  
  
    &lt;ol&gt;
      &lt;li&gt;Decision-agent&lt;/li&gt;
      &lt;li&gt;System dynamics&lt;/li&gt;
      &lt;li&gt;Information-processing :
        &lt;ol&gt;
          &lt;li&gt;Decision-agent&lt;/li&gt;
          &lt;li&gt;System dynamics&lt;/li&gt;
          &lt;li&gt;Information-processing&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  
  
    &lt;ul type="disc"&gt;
      &lt;li&gt;Networks :
        &lt;ul&gt;
          &lt;li type="circle"&gt;Decision-agent&lt;/li&gt;
          &lt;li type="circle"&gt;System dynamics&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is how it shows on device. One more thing that I would like to point out if I add paragraph styling from Swift side, then list shows like this, If I don't add paragraph styling then Lists but then padding more than my requirement. <a href="https://i.stack.imgur.com/UE7qZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UE7qZ.png" alt="Indentation issue of sublists on iOS"></a></p>