0

I have searched a lot but can only find HTML to plain text, not the other way around, I have email implementation in my app, thus need to send the content of email as HTML to the backend.

Edit 1: I have rich text that includes bold, italic, ordered/unordered list, underlined words.

user832
  • 796
  • 5
  • 18
  • what HTML thing are you referring to, will I have to create a function or something to convert it to HTML format. – user832 Jul 05 '17 at 11:08
  • 1
    `YOUR CONTENT HERE` Now your text is shown in xml / html. – luk2302 Jul 05 '17 at 11:12
  • @luk2302 actually I have a rich text i.e. bold, italic, ordered/unordered listing etc. I think I should mention this in my question too – user832 Jul 05 '17 at 11:54
  • 1
    ... which is basically the opposite of plain text. So you are asking how to convert formatted text into html that will look the same as the formatted one. – luk2302 Jul 05 '17 at 11:59
  • @user832 Are you looking for a way to convert `NSAttributedString` to html text? – Fangming Jul 05 '17 at 12:02
  • @FangmingNing yes i am looking for the same – user832 Jul 05 '17 at 12:20

2 Answers2

2

If you are looking to convert NSAttributedString to String, here is the extension method you are looking for. Simply call yourAttributtedString.htmlString() and print it out.

extension NSAttributedString {
    func htmlString() -> String? {
        let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
        do {
            let htmlData = try self.data(from: NSMakeRange(0, self.length), documentAttributes:documentAttributes)
            if let htmlString = String(data:htmlData, encoding:String.Encoding.utf8) {
                return htmlString
            }
        }
        catch {}
        return nil
    }
}
Fangming
  • 24,551
  • 6
  • 100
  • 90
  • i need to convert NSAttributedString to HTML – user832 Jul 05 '17 at 12:35
  • @user832 There is no HTML element in swift. I am converting `NSAttributedString` to a swift `String` object which is html string. When you send email out with this string, you will see it presented as html with your formating stuff – Fangming Jul 05 '17 at 12:41
  • it isn't detecting formatting like BOLD, ITALIC etc, just converting into HTML with paragraph attribute – user832 Jul 10 '17 at 06:48
  • @user832 that's because you haven't added any font yet. Try to set some bold font for your NSAttrbuttedDtring and try again – Fangming Jul 10 '17 at 09:20
0

According to this post:

private func getHtmlLabel(text: String) -> UILabel {
    let label = UILabel()
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    label.attributedString = stringFromHtml(string: text)
    return label
}

private func stringFromHtml(string: String) -> NSAttributedString? {
    do {
        let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
        if let d = data {
            let str = try NSAttributedString(data: d,
                                             options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                             documentAttributes: nil)
            return str
        }
    } catch { }
    return nil
}
Jimmy James
  • 825
  • 12
  • 28