mhinton11
mhinton11

Reputation: 15

Creating HTML email from UITextField text

I'm a novice and recreational programmer. I am trying to create an app for iOS that populates the text from my UITextFields. I have 3 the actionField, impactField, and result Field. I'd like for each to have their own line, if possible.

I looked at a similar answer, but it was not any help for me. Does anyone have any tutorials or advice?

- (IBAction)backgroundTap: (id)sender {
    [self.actionField resignFirstResponder];
    [self.impactField resignFirstResponder];
    [self.resultField resignFirstResponder];
}

- (IBAction)sendBullet:(id)sender {
    NSString *emailTitle = @"Email";
    NSString *messageBody = @"Title";
    NSArray *toRecipients = [NSArray arrayWithObject:@"email"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipients];

    [self presentViewController:mc animated:YES completion:NULL];

Upvotes: 0

Views: 205

Answers (2)

soulshined
soulshined

Reputation: 10602

MHinton,

Before you proceed, you should take a look at HTML syntax, there are hundreds of thousands of tutorials out there since this has been around for decades. It's fairly easy to pick up. You can start HERE

NOTE It's best to test HTML emails on a device as the simulator can be kind of wonky with the MFMailComposeViewController

Secondly, grabbing the text from a text view or other forms of strings is fairly easy, again, thousands of tutorials out there. Specifically for Obj C you do it as follows :

1) Create an outlet for your textfields

@interface YourViewController: UIViewController
@property (nonatomic, retain) IBOutlet UITextField *actionField
@end

2) Then you can reference it anywhere in your implementation file (.m) like this :

NSString *emailMessageBody = self.actionField.text;

DISPLAYING TEXT IN HTML FORMAT FOR EMAILS

Creating an HTML formatted email is relatively easy as long as you spend about 15 minutes learning HTML syntax.

If you want to insert your UITextFields in different lines you implement the HTML equivalent to line break (or pressing the enter button): <br>

So an example string to pass in the MFMailComposeViewController will look like this:

NSString *emailMessageBody = [NSString stringWithFormat:@"The retrieved text from actionField is : <br><strong><font color=\"red\">'%@'</font><br></strong><br> The retrieved text from impactField is : <br>%@<br> The retrieved text from resultField is : <br>%@<br>", self.actionField.text, self.impactField.text, self.resultField.text];

Your options are limitless here, as long as you know HTML syntax. So whats going on here?

  • <br> creates the line break
  • <strong> creates a bold style text and to end the format you place </strong> after the last word you want bold faced
  • <font> self explanatory; you have to end this style format as well.

You call it in the MFMailComposeViewController per usual, however you must explicitly set the email to HTML so all the HTML tags can be stripped from formatting and so it doesn't display it as plain text.

[mc setMessageBody:emailMessageBody isHTML:YES];

Other commonly used HTML tags are :

  • <style> for formatting
  • <p> for paragraphs
  • <h1> for headers
  • <mark> for highlighting
  • <small> for small text
  • <i> or <em> for italics or emphasized

Refer to the link above for more HTML tags


SWIFT SYNTAX

 var emailMessageBody = NSString(format:"The retrieved text from actionField is : <br><strong><font color=\"red\">'%@'</font><br></strong><br> The retrieved text from impactField is : <br>%@<br> The retrieved text from resultField is : <br>%@<br>", actionField.text, impactField.text, resultField.text) as String
 mailComposerVC.setMessageBody(emailMessageBody, isHTML: true)

Upvotes: 2

rmaddy
rmaddy

Reputation: 318804

All you need to do is build a string from the text fields:

NSString *messageBody = [NSString stringWithFormat:@"Action: %@\nImpact: %@\nResult: %@", self.actionField.text, self.impactField.text, self.resultField.text];

Update the format string as needed. What I present here is just one example.

The \n is a special value in a string that means "newline". This means the string I show will consist of three lines of text.

Upvotes: 1

Related Questions