My application uses NSAttributedString to display text to the user. Most of this text is inside the paragraph HTML tags (<p>
). When it's inside the tags, the font family is changed to some Times style.
It happend in the new version iOS 13, before we did not hve this problem.
On this image you can see normal font and the modified font when it is in the
tags: https://imgur.com/a/YY3Gdph
We don't know where to start looking for the solution.
let font = UIFont.preferredFont(forTextStyle: .body)
let strDesc: String = "\(explanation)\n"
let newStringDesc = strDesc.replacingOccurrences(of: "</ul>", with: "</ul><br>")
let modifiedString = "<style>body{font-family: '\(font.fontName)'; font-size:\(font.pointSize)px;}</style>\(strDesc)"
guard let data = modifiedString.data(using: .utf8) else { return nil }
let attrString = try NSMutableAttributedString(
data: data,
options: [
NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue,
],
documentAttributes: nil
)
attrString.addCustomParagraphSettings(font: font)
attrString.setCustomParagraphFontColor()
let mainAttributedText = NSMutableAttributedString()
mainAttributedText.append(attrString)
mainAttributedText.append(tmpTakenText)
descriptionTextView.attributedText = mainAttributedText
I expect to see standard iOS font.
@Larme found a bug in the code. The value of the font-family
CSS property should be font.familyName
and not font.fontName
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With