问题
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