UILabel text in email body

Discussion in 'iOS Programming' started by nickculbertson, Jan 28, 2011.

  1. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #1
    In the following code:

    Code:
    - (IBAction)email:(id)sender{
    	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
    													message:@"message" 
    												   delegate:self 
    										  cancelButtonTitle:@"Thanks! Maybe Later" 
    										  otherButtonTitles:@"Email Name", @"Review",nil];
    
    	[alert show];
    	[alert release];
    }
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    	if (buttonIndex == 2){
    //
    	}if (buttonIndex == 1){
    		MFMailComposeViewController *picker = 
    		[[MFMailComposeViewController alloc] init];
    		picker.mailComposeDelegate = self;
    		[picker setSubject:@"Name"];  
    		
    		
    		NSString *emailBody = @"Hello my name is _______ _______ . What is yours?"; 
    		[picker setMessageBody:emailBody isHTML:YES];
    		[self presentModalViewController:picker animated:YES];
    		[picker release];
    		
    	}if (buttonIndex == 0){
    //		
    	}
    }
    
    - (void)mailComposeController:(MFMailComposeViewController*)controller
              didFinishWithResult:(MFMailComposeResult)result
                            error:(NSError*)error {
    	[self becomeFirstResponder];
    	[self dismissModalViewControllerAnimated:YES];
    }
    
    is it possible to add the text from 2 UILabels into the blanks here:

    Code:
    NSString *emailBody = @"Hello my name is _______ _______ . What is yours?";
    ?

    Thanks,
    Nick
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Yes. NSString's stringWithFormat: is perfectly suited to this need. Make sure you check out the String Programming Guide for more information.
     
  3. thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #3
    Thank you, again. I'll read over the documentation this weekend.
     
  4. thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #4
    Alright, I got it to work with stringWithFormat:

    Code:
    NSString *emailBody = [NSString stringWithFormat:@"Hello,   %@ %@ %@ %@ .  Goodbye", textview.text, textview2.text, textview3.text, textview4.text];
    Is there a way to have the resulting text appear on multiple lines?

    Code:
    Hello,
       My Name Is     .
    Goodbye
    Thanks,
    Nick
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Yeah, just use \n (the newline specifier) in your format string.
     
  6. nickculbertson, Feb 3, 2011
    Last edited by a moderator: Feb 3, 2011

    thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #6
    Thanks, you are correct. I had tried using \n but I couldn't get it to work. As it turns out I coded the line break the right way but it just didn't translate properly in the email body. I ran

    Code:
    NSLog([NSString stringWithFormat:@"Hello, \n    %@ %@ %@ %@ .  \n  Goodbye.", textview.text, textview2.text, textview3.text, textview4.text]);
    it starts a new line at \n. however in

    Code:
    NSString *emailBody = [NSString stringWithFormat:@"Hello, \n    %@ %@ %@ %@ .  \n  Goodbye.", textview.text, textview2.text, textview3.text, textview4.text];
    the email text appears on one line.

    Oh well, it isn't totally necessary for my project. I just thought I'd add some ascii art.

    EDIT:

    Ah ha! this line was in the way.

    Code:
    [picker setMessageBody:emailBody isHTML:YES];
    changed it to

    Code:
    [picker setMessageBody:emailBody isHTML:NO];
    Thanks,
    Nick
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Well, if you want HTML, you can just use <br> instead of \n.
     

Share This Page