I found this string extension somewhere on SO that allows me to turn html code into an attributed string:
func html2AttributedString() -> NSAttributedStri
Swift 4.x & 5.x
if let rtfPath = Bundle.main.url(forResource: "FileName", withExtension: "rtf") {
do {
let attributedString: NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
debugPrint(attributedString)
} catch {
print("Error while reading the file - \(error.localizedDescription)")
}
}
Getting NSAttributedString from file will be changed in Swift 3.x which is as below:
let attributedStringWithRtf: NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
all the other code same as Swift 4.x & 5.x.
For more details please read the Apple document related to the NSAttributedStringDocumentType