I have these extensions to parse string with html tags to NSAttributedString
extension StringProtocol {
var html2AttributedString: NSAttributedString? {
Data(utf8).html2AttributedString
}
}
extension Data {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(
data: self,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
],
documentAttributes: nil)
} catch {
bfprint("html2AttributedString: Html string parsing error: \(error)",
tag: LogUtils.Tags.criticalDevError,
level: .error)
return nil
}
}
}
The app gets original html strings from Localizable files. Sometimes when the app parses the html strings with <u> tag like this "This is <u>underlined part</u>" I see this error log:
html2AttributedString: Html string parsing error: Error Domain=NSCocoaErrorDomain Code=259 "The file couldn’t be opened because it isn’t in the correct format."
I have read official doc and didn't find any pitfalls that could cause the problem. Also I didn't find any similar questions related to getting NSAttributedString