解码HTML字符串

时间:2016-10-18 16:46:20

标签: html swift decode

如何解码我的html字符串:

<span>Bj&ouml;rn</span>

<span>Björn</span>

在Swift 3中?

1 个答案:

答案 0 :(得分:13)

在替换<span>符号时,您真的需要保留&ouml;代码吗? Leo Dabus在Convert Unicode symbol or its XML/HTML entities into its Unicode number in Swift中建议的一种技术转换符号包括通过属性字符串将其往返。

在Swift 4中:

extension String {
    /// Converts HTML string to a `NSAttributedString`

    var htmlAttributedString: NSAttributedString? {
        return try? NSAttributedString(data: Data(utf8), options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
    }
}

如果您想要一个属性字符串(例如,用于UILabel

let string = "Bj&ouml;rn is <em>great</em> name"
label.attributedText = string.htmlAttributedString

这会将Bj&ouml;rn转换为Björn并将<em>...</em>部分设为斜体。

如果您只想转换HTML符号并删除HTML标记(例如<span> / </span>),只需抓取string

let string = "Bj&ouml;rn is <em>great</em> name"
if let result = string.htmlAttributedString?.string {
    print(result)   // "Björn is great name"
}

对于之前的Swift版本,请参阅此答案的previous revision