title: How to convert AttributedString to NSMutableString Swift?
body: <p>I have HTML and was converted to AttributedString. Now, I need to change the generated Attributed string's font but I'm having a hard time retaining the style(Bold, Italic or Regular). </p>
  
  <p>I found a solution but the problem is I don't know how to use it. They using NSMutableAttributedString as extension. I pasted my code how did I convert and the supposed solution at the bottom.</p>
  
  <p>Thank you.</p>
  
  <pre><code>extension String {
  
   var htmlToAttributedString: NSAttributedString? {
          guard let data = data(using: .utf8) else { return NSAttributedString() }
          do {
              return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
          } catch {
              return NSAttributedString()
          }
      }
  
  }
  
  
  
  
  
  
  import Foundation
  
  struct Service: Codable {
  
      var id: Int
      var name: String?
      var price: String?
      var description: String?
      var subtitle: String?
      var bodyPreview: String?
      var featuredImage: String? // For FindAll
      var imageList: [String]? // For FindByID
  
      private enum CodingKeys: String, CodingKey {
          case id
          case name
          case price
          case subtitle
          case description
          case bodyPreview = "body_preview"
          case featuredImage = "featured_image_url"
          case imageList = "images_url"
      }
  
  }
  
  
  
  class ServiceDetailViewController: UIViewController {
  
  
      private var service: Service?
  
      private func showServiceDetails() {
          detailLabel.attributedText = service?.description?.htmlToAttributedString
          collectionView.reloadData()
          startCollectionViewTimer()
      }
  
  }
  </code></pre>
  
  <p>Manmal's solution:</p>
  
  <pre><code>extension NSMutableAttributedString {
      func setFontFace(font: UIFont, color: UIColor? = nil) {
          beginEditing()
          self.enumerateAttribute(
              .font,
              in: NSRange(location: 0, length: self.length)
          ) { (value, range, stop) in
  
              if let f = value as? UIFont,
                  let newFontDescriptor = f.fontDescriptor
                      .withFamily(font.familyName)
                      .withSymbolicTraits(f.fontDescriptor.symbolicTraits) {
  
                  let newFont = UIFont(
                      descriptor: newFontDescriptor,
                      size: font.pointSize
                  )
                  removeAttribute(.font, range: range)
                  addAttribute(.font, value: newFont, range: range)
                  if let color = color {
                      removeAttribute(
                          .foregroundColor,
                          range: range
                      )
                      addAttribute(
                          .foregroundColor,
                          value: color,
                          range: range
                      )
                  }
              }
          }
          endEditing()
      }
  }
  </code></pre>