How do i convert NSAttributedString into HTML string?

后端 未结 2 1073
闹比i
闹比i 2020-11-29 04:53

As the title tells,now i can simple convert HTML into NSAttributedString with initWithHTML:documentAttributes: , but what i want to do here is reve

2条回答
  •  感情败类
    2020-11-29 05:17

    This is a swift 4 conversion of @omz answer, hope is useful to anyone landing here

    extension NSAttributedString {
        var attributedString2Html: String? {
            do {
                let htmlData = try self.data(from: NSRange(location: 0, length: self.length), documentAttributes:[.documentType: NSAttributedString.DocumentType.html]);
                return String.init(data: htmlData, encoding: String.Encoding.utf8)
            } catch {
                print("error:", error)
                return nil
            }
        }
    }
    

提交回复
热议问题