NSAttributedString conversion: Bold, <b>-tag, not working

I have a String extension function which converts an html string to a NSAttributed string, and it's made up with Swift 3.x. The strings I'm showing in a UILabel consists of different html-tags, like <i>, <b>, and <center>.


Using Xcode 9.0.1 and deploying the app with the code onto an iPhone running iOS 10 it's working perfectly.


Same app, but deployed to an iPhone running iOS 11, all html-tags are converted - except the <b>.


Here's the code:

    func htmlAttributedString(fontSize: CGFloat = 17.0) -> NSAttributedString? {
        let fontName = UIFont.systemFont(ofSize: fontSize).fontName
        let string = self.appending(String(format: "<style>body{font-family: '%@'; font-size:%fpx;}</style>", fontName, fontSize))
        guard let data = string.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
    
        guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
            else {
                return nil
        }
    
        print("\(String(describing: self)):\(#function): attributedString: \(attributedString)")
        return attributedString
    }


By looking at the attributedString output to the console, I can see that on the iPhone with iOS 10 the attributed string contains .SFUIText, .SFUIText-Italic, and .SFUIText-BoldItalic as it should, but running iOS 11 the .SFUIText-BoldItalic is missing.


Have any one else experienced the same issue, and maybe has come up with a solution?


//Mellberg

For anyone experiencing the same issue, I've found a (somewhat limiting) solution.


Trying to change from the system font to another one did not solve the problem, but hardcoding it like below made it work more satisfying. The only problem is that the system font cannot be hardcoded like this, I think. Instead I decided to use ArialMT.


let string = self.appending(String(format: "<style>body{font-family: 'ArialMT'; font-size:%fpx;}</style>", fontSize))


If any one have a better solution, please share. :-)


//Mellberg

If you want the system font, the trick seems to be to use the method you outlined and set the font-family to "-apple-system".

NSAttributedString conversion: Bold, &lt;b&gt;-tag, not working
 
 
Q