Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why iOS13 changes the font-family inside NSAttributedString for paragraphs <p>

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.

like image 725
Grzegorz Świerad Avatar asked Sep 13 '19 14:09

Grzegorz Świerad


1 Answers

@Larme found a bug in the code. The value of the font-family CSS property should be font.familyName and not font.fontName.

like image 97
Grzegorz Świerad Avatar answered Sep 30 '22 20:09

Grzegorz Świerad