将 html 转换为字符串 - 尾随新行

Converting html to String - trailing new line

我正在开发一个应用程序,我从端点获取数据,然后将它们显示给用户。我得到的大部分“字符串”数据都是 html 格式,所以我需要将它转换成字符串。一切正常,除了删除尾随的换行符。这些是我正在使用的扩展:

数据扩展名:

extension Data {
    var htmlToAttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print("error:", error)
            return  nil
        }
    }
    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
}

字符串扩展:

extension String {
    var htmlToAttributedString: NSAttributedString? {
         return Data(utf8).htmlToAttributedString
    }
    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
}

代码示例:

let descriptionHtml = "\n<p>Rømø beach is still a young beach and created from blown sand so fine that it resembles tiny diamonds. All year round, from morning to evening, there will be activity on the beach, known as one of Northern Europe&#8217;s absolute best and widest.</p>\n"
let descriptionAsString = descriptionHtml.htmlToString

//Expected result: "Rømø beach is still a young beach and created from blown sand so fine that it resembles tiny diamonds. All year round, from morning to evening, there will be activity on the beach, known as one of Northern Europe’s absolute best and widest."
//  Actual result: "Rømø beach is still a young beach and created from blown sand so fine that it resembles tiny diamonds. All year round, from morning to evening, there will be activity on the beach, known as one of Northern Europe’s absolute best and widest.\n"
//

我可以通过将 "\n" 替换为 "" 来简单地解决这个问题,但我似乎不是一个温和的解决方案,因为我使用的是实际的 NSAttributedString 初始化器,这让我感到惊讶,它不起作用正如我所期望的那样。有什么原因导致它在末尾留下换行符,有没有办法在不手动替换换行符的情况下修复它?

此文本包含在 <p> 标记中。如果它被呈现,通常会以换行符结尾。这应该是预期的行为。如果您不希望它成为“paragraph-like”,那么它不应该在 <p> 标签中。

我删除了 <p></p>,但保留了最后的 \n,它将以尾随 space 呈现,这是 [= 中呈现换行符的正常方式19=]。同样,这应该是预期的行为。您正在渲染 HTML。这允许您以块的形式呈现 HTML 并以合理的方式将生成的字符串粘合在一起。