Email Composer with Attributed String

不打扰是莪最后的温柔 提交于 2020-01-24 11:45:45

问题


How can I send attributed string via Email?

mailComposerVC.setMessageBody(textView.attributedString, isHTML: false)

回答1:


You have convert your Attributed string to HTML string. Use following code to generate html string.

        do {
        let data = try string.dataFromRange(NSMakeRange(0, string.length), documentAttributes: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType])
        let htmlString = String(data: data, encoding: NSUTF8StringEncoding)
    }catch {

    }

Use generated html string as

mailComposerVC.setMessageBody(htmlString, isHTML: true)



回答2:


Thanks guys, this is how it's done:

    var cookedString : String!
    let attributeStrung = myTextView.attributedText

    let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
    do
    {
        let htmlData = try attributeStrung.dataFromRange(NSMakeRange(0, attributeStrung.length), documentAttributes: documentAttributes)
        if let htmlString = String(data: htmlData, encoding: NSUTF8StringEncoding)
        {
            cookedString = htmlString
        }
    }
    catch
    {
        print("error creating HTML from Attributed String")
    }

    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setMessageBody(cookedString, isHTML: true)


来源:https://stackoverflow.com/questions/35843883/email-composer-with-attributed-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!