I have a string extension that converts HTML to an NSAttributedString, but i add my own custom font to the HTML string from an API before formatting in the following way.
let testString = "<span style="font-family: MuseoSans-900"> This is an html String <i> with italics </i> and a custom font </span>"
guard let htmlData = testString.data(using: .utf8) else { return nil }
do {
let attrString = try NSAttributedString(data: htmlData, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue,
], documentAttributes: nil)
let formattedHTMLString = NSMutableAttributedString(attributedString: HTMLString)
formattedHTMLString.beginEditing()
formattedHTMLString.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, formattedHTMLString.length), options: []){
(_ value: Any?, _ range: NSRange, _ stop: UnsafeMutablePointer) -> Void in
if value != nil {
var oldFont = value as? UIFont
print("\(oldFont?.fontName)")
}
}
return formattedHTMLString
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
On iOS 10 < the attributed string is returned with the entire string having the same font (in this case MuseoSans-900) including the italized part of the html string but on iOS 11 it updates and changes the font to MuseoSans-300 for the italics part of the html string and keeps the rest of the string MuseoSans-900 during the conversion to NSAttributed string..I do not know why this is happening and any help would be greatly appreciated.