I have an RTF file with which I was able to get contents as Data (i want it to be in Data to use it elsewhere in code). Howeever, I would like to add styling to the attributedString like font before assigning to label. Im struck on how to apply these attributes in below code. Please advice how i can achieve same. Here's my code
let rtfData = getRTFData()
guard let data = rtfData else {
return
}
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil) {
myLabel.attributedText = attributedString
}
Here i wanted to add attributes to attributedString like
let boldAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 14)]
and apply these attributes to attributedText
If you want to append font attribute to existing attributes this one should work:
if let attributedString = try? NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil) {
var attrs = attributedString.attributes(at: 0, effectiveRange: nil)
attrs[NSAttributedString.Key.font] = UIFont.boldSystemFont(ofSize: 14)
attributedString.setAttributes(attrs, range: NSRange(location: 0, length: attributedString.length))
myLabel.attributedText = attributedString
}