带有属性字符串的电子邮件编辑器

Email Composer with Attributed String

如何通过电子邮件发送属性字符串?

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

谢谢大家,这是完成的方式:

    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)

您已将属性字符串转换为 HTML 字符串。使用以下代码生成 html 字符串。

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

    }

使用生成的 html 字符串作为

mailComposerVC.setMessageBody(htmlString, isHTML: true)

更新为 swift 5 Xcode 12.1

var cookedString : String!
let attributeString = myTextView.attributedText

    let documentAttributes = [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html]
        do
        {
            let htmlData = try attributeString!.data(from: NSMakeRange(0, attributeStrung!.length), documentAttributes: documentAttributes)
            if let htmlString = String(data: htmlData, encoding: String.Encoding.utf8)
            {
                cookedString = htmlString
            }
        }
        catch
        {
            print("error creating HTML from Attributed String")
        } let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setMessageBody(cookedString, isHTML: true)