Post

Replies

Boosts

Views

Activity

Reply to iOS 15 Crash on [WebLowPowerModeObserver _didReceiveLowPowerModeChange] when convert HTML to NSAttributedString
Add more information, the converting HTML to NSAttributedString function is like convenience public init(htmlString html: String, font: UIFont? = nil, useDocumentFontSize: Bool = true) throws {   let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [    .documentType: NSAttributedString.DocumentType.html,    .characterEncoding: String.Encoding.utf8.rawValue   ]   guard let data = html.data(using: .utf8, allowLossyConversion: true) else {    self.init(string: "")    return   }   guard let fontFamily = font?.familyName else {    try self.init(data: data, options: options, documentAttributes: nil)    return   }   let attr = try NSMutableAttributedString(data: data, options: options, documentAttributes: nil)   let fontSize: CGFloat? = useDocumentFontSize ? nil : font!.pointSize   let range = NSRange(location: 0, length: attr.length)   attr.enumerateAttribute(.font, in: range, options: .longestEffectiveRangeNotRequired) { attrib, range, _ in    if let htmlFont = attrib as? UIFont {     let traits = htmlFont.fontDescriptor.symbolicTraits     var descrip = htmlFont.fontDescriptor.withFamily(fontFamily)     if (traits.rawValue & UIFontDescriptor.SymbolicTraits.traitBold.rawValue) != 0 {      descrip = descrip.withSymbolicTraits(.traitBold)!     }     if (traits.rawValue & UIFontDescriptor.SymbolicTraits.traitItalic.rawValue) != 0 {      descrip = descrip.withSymbolicTraits(.traitItalic)!     }     attr.addAttribute(.font, value: UIFont(descriptor: descrip, size: fontSize ?? htmlFont.pointSize), range: range)    }   }   self.init(attributedString: attr)
Sep ’21