NSString 形式的 HTML 电子邮件模板

发布于 2024-08-24 19:13:42 字数 532 浏览 13 评论 0原文

我正在向我的应用程序添加功能,使其能够通过电子邮件发送一些相当详细的信息。我想将其作为 HTML 格式的电子邮件发送,以便我可以利用 HTML 和 CSS 样式。

我想要采取的方法是设计带有占位符的模板来填充字段,将模板加载到 NSString 中,然后使用字符串替换来用实际数据更改占位符。

而不是只有一句胡言乱语,例如:

NSString *emailTemplate = [NSString stringWithFormat:@"<h1>TITLE</h1><hr noshade/><p>Here is the information about the thing that you are interested in. Find more from the table below:</p> . . ."]

我想以一种更优雅的方式做到这一点。我应该将模板写入文件然后加载吗?最好的方法是什么?

另外,我怎样才能以可本地化的方式做到这一点?

谢谢!

I am adding functionality to my app that will allow it to email some rather detailed information. I would like to send this as an HTML-formatted email so that I can take advantage of HTML and CSS styling.

The approach I'd like to take is to design the template with placeholders for the fields to be filled in, load the template into an NSString, then use string replacement to change out the placeholders with the actual data.

Rather than having one huuuuuuge line such as:

NSString *emailTemplate = [NSString stringWithFormat:@"<h1>TITLE</h1><hr noshade/><p>Here is the information about the thing that you are interested in. Find more from the table below:</p> . . ."]

I'd like to do this in a more elegant way. Should I write the template into a file then load it? What's the best way to do this?

Also, how could I do this in a localize-able way?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-08-31 19:13:42

我不确定以可本地化的方式执行此操作的最佳方法,但将 HTML 写入它自己的 .html 文件,并将该文件的内容读入 对您来说会更有利NSMutableString 对象,当你需要它的时候。

您可以使用以下方法来完成此操作:

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error

如果使用此方法,则需要对返回的字符串调用 mutableCopy,以便稍后可以进行字符串替换。不要忘记,使用 mutableCopy 将返回一个新的 NSMutableString 对象,您将负责在使用完毕后将其释放。

然后您可以继续使用

- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange

将占位符替换为您的内容。

这样做的优点意味着您可以使用所见即所得编辑器或您最喜欢的 IDE 轻松编辑 HTML,而无需更改应用程序中的代码。您还可以通过这种方式更轻松地预览它的外观。

I'm not sure about the best way to do this in a localisable way, but it would be a lot more beneficial for you to write the HTML into it's own .html file, and read the contents of that file into an NSMutableString object when you need it.

You can do this using

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error

If you use this method, you'll need to call mutableCopy on the returned string, so that you can do the string replacement later. Don't forget that using mutableCopy will return a new NSMutableString object that you'll be responsible for releasing when you're finished with it.

Then you can go ahead and use

- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange

To replace the placeholders with your content.

The advantages of doing it this way means that you can easily edit the HTML using a WYSIWYG editor, or your favourite IDE, without changing the code in your app. You can also preview what it'll look like much easier this way.

浮华 2024-08-31 19:13:42

看一下 Matt Gemmell 的实现,MGTemplateEngine。它适用于 OS X 和 iOS。原始版本似乎仅托管在 SVN 上,因此有人 git-svn 将其编辑到 Github

我需要在编译之前修改构建设置,但它的工作方式就像一个魅力,并且非常强大(事实上,比我更强大,想必你也需要)。

Take a look at Matt Gemmell’s implementation, MGTemplateEngine. It works on OS X and iOS. The original seems to be hosted on SVN only, so someone git-svn'ed it to Github.

I needed to tinker with the build settings before it compiled, but it works like a charm and is pretty powerful (indeed, way more powerful than I, and presumably also you, needed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文