Search code examples
iosswiftemailnsattributedstring

Email Composer with Attributed String


How can I send attributed string via Email?

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

Solution

  • 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)