Fixes #1418 - Preserve new lines when parsing html strings

This commit is contained in:
Stefan Ceriu
2023-08-08 17:17:13 +03:00
committed by Stefan Ceriu
parent 0511bef33a
commit 35685db3c4
2 changed files with 19 additions and 2 deletions
@@ -56,8 +56,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
// that could happen with the default HTML renderer of NSAttributedString which is a
// webview.
func fromHTML(_ htmlString: String?) -> AttributedString? {
guard let htmlString,
let data = htmlString.data(using: .utf8) else {
guard let htmlString else {
return nil
}
@@ -65,6 +64,13 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
return cached
}
// Trick DTCoreText into preserving newlines
let adjustedHTMLString = htmlString.replacingOccurrences(of: "\n", with: "<br>")
guard let data = adjustedHTMLString.data(using: .utf8) else {
return nil
}
let defaultFont = UIFont.preferredFont(forTextStyle: .body)
let parsingOptions: [String: Any] = [