title: Resizing UILabel with HTML data
body: <p>I created a UILabel and synced its font size with a seekbar and loaded my html formatted text onto the UILabel. Unfortunately, the format gets removed if I change the font size of my UILabel using seekbar.</p>
  
  <p>I used this approach, from garie, that was answered from another thread</p>
  
  <pre><code>private func getHtmlLabel(text: String) -&gt; UILabel {
      let label = UILabel()
      label.numberOfLines = 0
      label.lineBreakMode = .byWordWrapping
      label.attributedString = stringFromHtml(string: text)
      return label
  }
  
  private func stringFromHtml(string: String) -&gt; NSAttributedString? {
      do {
          let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
          if let d = data {
              let str = try NSAttributedString(data: d,
                                               options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                               documentAttributes: nil)
              return str
          }
      } catch {
      }
      return nil
  }
  </code></pre>
  
  <p>Link: <a href="https://stackoverflow.com/questions/37048759/swift-display-html-data-in-a-label-or-textview">Swift: Display HTML data in a label or textView</a></p>
  
  <p>Is there any approach I could use to retain the my html formatted text?</p>
  
  <p><strong>EDIT #1:</strong>
  This is how I resize my UILabel.</p>
  
  <pre><code>@IBOutlet weak var textLabel: UILabel!
  @IBAction func resizeText(_ sender: UISlider) {
      textLabel.font = UIFont.systemFont(ofSize: CGFloat(sender.value) * 20.0)
  }
  </code></pre>
  
  <p>The HTML data I'm passing onto my UILabel:</p>
  
  <pre><code>&lt;html&gt;&lt;body&gt;&lt;b&gt;hello world&lt;/b&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>This is the formatted HTML data on UILabel:</p>
  
  <p><a href="https://i.stack.imgur.com/hmX8Q.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hmX8Q.jpg" alt="enter image description here"></a></p>
  
  <p>After resizing the UILabel, it loses its format:</p>
  
  <p><a href="https://i.stack.imgur.com/S2owp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S2owp.jpg" alt="enter image description here"></a></p>
  
  <p><strong>EDIT #2:</strong></p>
  
  <p>Tried Milan's answer but encountered an error. Unresolved identifier in NSAttributedStringKey</p>
  
  <p>Here's my code:</p>
  
  <pre><code>import UIKit
  
  class GalleryViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          textLabel.attributedText = stringFromHtml()
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
      }
  
      @IBOutlet weak var textLabel: UILabel!
      @IBAction func resizeText(_ sender: UISlider) {
          if let attributedText = textLabel.attributedText {
              let newAttributedText = NSMutableAttributedString(attributedString: attributedText)
              newAttributedText.setFontSize(newSize: CGFloat(sender.value) * 20.0)
              textLabel.attributedText = newAttributedText
          }
      }
  
  
      private func stringFromHtml() -&gt; NSAttributedString? {
          do {
              let string = "&lt;html&gt;&lt;body&gt;&lt;b&gt;hello world&lt;/b&gt;&lt;/body&gt;&lt;/html&gt;"
              let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
              if let d = data {
                  let str = try NSAttributedString(data: d,
                                                   options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                   documentAttributes: nil)
                  return str
              }
          } catch {
          }
          return nil
      }
  }
  
  extension NSMutableAttributedString {
      func setFontSize(newSize: CGFloat) {
          beginEditing()
          self.enumerateAttribute(NSAttributedStringKey.font, in: NSRange(location: 0, length: self.length)) { (value, range, stop) in
              if let f = value as? UIFont {
                  let newFont = f.withSize(newSize)
                  removeAttribute(NSAttributedStringKey.font, range: range)
                  addAttribute(NSAttributedStringKey.font, value: newFont, range: range)
              }
          }
          endEditing()
      }
  }
  </code></pre>