String formatting in cocoa

Posted by lostInTransit on Stack Overflow See other posts from Stack Overflow or by lostInTransit
Published on 2010-03-09T09:36:44Z Indexed on 2010/03/09 10:06 UTC
Read the original article Hit count: 728

Filed under:


I am trying to send some text in an email from my cocoa app (by using Initially I tried using HTML to send properly formatted text. But the mailto: URL does not support html tags (even after setting headers)

So I decided to use formatted string (left-aligning of string) This is what I have in my mailto: link's body argument

NSMutableString *emailBody = [NSMutableString stringWithFormat:@"|%-35s", [@"Name" UTF8String]];
[emailBody appendFormat:@"|%-18s", [@"Number" UTF8String]];
[emailBody appendString:@"|Notes\n"];

[emailBody appendString:@"----------------------------------------------------------------------------------------------------"];

for(int i = 0; i < [items count]; i++){
    NSDictionary *props = [items objectAtIndex:i];

    NSMutableString *emailData = [NSMutableString stringWithFormat:@"|%-35s", [[props valueForKey:@"name"] UTF8String]];

    [emailData appendFormat:@"|$ %-16s", [[props valueForKey:@"number"] UTF8String]];
    [emailData appendString:[props valueForKey:@"notes"]];

    [emailBody appendString:@"\n"];
    [emailBody appendString:emailData];

This does give me padded text but they all don't necessarily take up the same space (for instance if there is an O in the text, it takes up more space than others and ruins the formatting)

Is there another sure-shot way to format text using just NSString?


© Stack Overflow or respective owner

Related posts about cocoa

Related posts about nsstring