如何在电子邮件正文中使文本变为“粗体”

时间:2019-08-24 07:05:43

标签: ios swift xcode

我有填充在电子邮件正文中的文本字段。 问题-如何使名字和姓氏以粗体显示(在发送的电子邮件中)?

这是我的电子邮件代码

    // Send Email ----------------------------
    @IBAction func SendEmail(_ sender: UIButton) {

        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.setPreferredSendingEmailAddress(EmailFrom.text ?? "")
            mail.mailComposeDelegate = self
            mail.setToRecipients([DefaultEmail.text ?? ""])
            mail.setSubject(DefaultSubject.text ?? "")
            mail.setMessageBody("\(String(describing: DateTextField.text ?? "nil"))</br></br>\n\(String(describing: FirstName.text ?? "nil"))\n\(String(describing: LastName.text ?? "nil"))</br></br>\n\(String(describing: Street.text ?? "nil"))</br>\n\(String(describing: Area.text ?? "nil"))\n\(String(describing: PostCode.text ?? "nil"))</br></br>\nPhone: \(String(describing: Phone.text ?? "nil"))</br>\nEmail: \(String(describing: Email.text ?? "nil"))</br></br>\nReference: \(String(describing: Reference.text ?? "nil"))</br>\nOther Details: \(String(describing: OtherDetailsField.text ?? "nil")) ", isHTML: true)

            present(mail, animated: true)
        } else {
            print("Application is not able to send an email")

        }

        }

//MARK: MFMail Compose ViewController Delegate method
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {

        // Dismiss the mail compose view controller.
        controller.dismiss(animated: true, completion: nil)
    }

2 个答案:

答案 0 :(得分:2)

这是我拥有的代码

mail.setMessageBody("\(String(describing: FirstName.text ?? "nil"))\n\(String(describing: LastName.text ?? "nil"))

这是修改后的代码

mail.setMessageBody("\<strong>(String(describing: FirstName.text ?? "nil"))\n\(String(describing: LastName.text ?? "nil"))</strong>

答案 1 :(得分:1)

在邮件内容中使用<strong><b>标签。将文本包装在标签内,无论您想以粗体显示。但您的邮件标题中应包含Content-Type: text/html,否则,电子邮件将不会显示粗体并将其视为纯文本。当您将电子邮件的内容类型设置为text/html时,您将可以在邮件正文中使用Html标签,然后将它们呈现。