开发者

String formatting in cocoa

I am trying to send some text in an email from my cocoa app (by using Mail.app). 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?

Thanks


No matter what content you send via a mailto: URL, its formatting will be determined by the user's settings in Mail. The default setting uses a proportional font and aligning text with spaces when a proportional font is being used is doomed to failure. You can't force Mail to use a monospace font via a mailto: URL. You can only send a plain string to Mail this way.

In general, mailto: links should be used for creating only very basic email content.

If you want to control the formatting of the email, you should create an HTML email as outlined in this answer to a previous question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜