我有一封电子邮件的 HTML 包含在一个 NSString 中,并希望将前几行文本显示为预览。我尝试了许多不同的方法来尝试从中获取有意义的内容,但似乎找不到有效的解决方案。我注意到 iOS 在显示消息列表的视图和锁定屏幕上做得很好。
我想知道是否有办法解析电子邮件的内容,像这样?
好吧,对于我发送给自己的一封简单的测试电子邮件,没有有意的 html 格式:
<div style="padding-bottom: 20px;"><div style="background-color:#eee"> <div><b>From:</b> Andrew <my@emailaddress.com></div> <div><b>To:</b> Andrew <My@emailaddress.com></div> <div><b>Subject:</b> This is an email</div> <div><b>Date:</b> 14 June 2013 16:44:00 BST</div> </div></div><div><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for HTML5 (experimental) for Mac OS X https://github.com/w3c/tidy-html5/tree/c63cc39" />
<title></title>
</head>
<body>
<div dir="ltr">Hey Andrew,
<div><br /></div>
<div>I haven't seen you in so long. Anyway, i wanted to ask you
about something.</div>
<div><br /></div>
<div style="">It's really important.</div>
</div>
</body>
</html>
</div>
在邮件应用程序上,Apple 显示了以下内容的预览:
嘿,安德鲁,我好久没见到你了。无论如何,我想问你一些事情...
然后从订阅电子邮件:
<div style="padding-bottom: 20px;"><div style="background-color:#eee"> <div><b>From:</b> Sidebar <hello@sidebar.io></div> <div><b>To:</b> Me@emailaddress.com</div> <div><b>Subject:</b> Front-End Design Conf, iOS 7, Secret Handshake, NSA Slides, Award-Winning Work</div> <div><b>Date:</b> 14 June 2013 08:59:30 BST</div> </div></div><div><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for HTML5 (experimental) for Mac OS X https://github.com/w3c/tidy-html5/tree/c63cc39" />
<meta http-equiv="Content-Type" content=
"text/html; charset=utf-8" />
<title>Front-End Design Conf, iOS 7, Secret Handshake, NSA Slides,
Award-Winning Work</title>
<style type="text/css">
/*<![CDATA[*/
#outlook a{
padding:0;
}
.ReadMsgBody{
width:100%;
}
.ExternalClass{
width:100%;
}
body{
margin:0;
padding:0;
}
//Loads of css stuff here
/*]]>*/
</style>
</head>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0"
offset="0" style="margin: 0;padding: 0;background-color: #F4F4F4;">
<center>
<table border="0" cellpadding="0" cellspacing="0" height="100%"
width="100%" id="bodyTable" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;margin: 0;padding: 0;background-color: #F4F4F4;height: 100% !important;width: 100% !important;">
<tr>
<td align="center" valign="top" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<!-- // BEGIN PREHEADER -->
<table border="0" cellpadding="10" cellspacing="0" width="100%" id=
"templatePreheader" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;background-color: #f36c3d;border-bottom: 0;">
<tr>
<td align="center" valign="top" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<table border="0" cellpadding="0" cellspacing="0" width="600"
style="border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td valign="top" class="preheaderContent" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;color: #FFFFFF;font-family: Helvetica;font-size: 10px;line-height: 125%;text-align: left;">
The 5 best design links every day, curated by a selection of great
editors.</td>
<!--
-->
<td valign="top" width="220" class="preheaderContent" style=
"padding-left: 20px;border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;color: #FFFFFF;font-family: Helvetica;font-size: 10px;line-height: 125%;text-align: left;">
Email not displaying correctly?<br />
<a href=
"http://us2.campaign-archive2.com/?u=b5af47765edbd2fc173dbf27a&id=c214e283a3&e=4fa588f2fc"
target="_blank" style=
"color: #FFFFFF;font-weight: normal;text-decoration: underline;">View
it in your browser</a>.</td>
<!--
--></tr>
</table>
</td>
</tr>
</table>
<!-- END PREHEADER \\ -->
<!-- // BEGIN TEMPLATE SECTIONS -->
<table border="0" cellpadding="0" cellspacing="0" width="100%"
style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td align="center" valign="top" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<!-- // BEGIN HEADER -->
<table border="0" cellpadding="0" cellspacing="0" width="100%"
style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td align="center" valign="top" id="templateHeader" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;background-color: #FFFFFF;border-top: 0;border-bottom: 0;">
<table border="0" cellpadding="0" cellspacing="0" width="600"
class="templateContainer" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td class="headerContent" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;color: #505050;font-family: Helvetica;font-size: 20px;font-weight: bold;line-height: 100%;padding-top: 20px;padding-right: 0;padding-bottom: 20px;padding-left: 0;text-align: left;vertical-align: middle;">
<br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=9b41202aa5&e=4fa588f2fc"
target="_self" style=
"text-decoration: none;color: #26ABE2;font-weight: normal;"><span style="color:#f36c3d; font-size:48px;">
Sidebar</span></a><br />
<br />
<h2 style=
"margin-bottom: 0px;padding: 0px;border: 0px;font: inherit;font-size: 18px;vertical-align: baseline;font-weight: 500;line-height: 1;color: rgb(170, 170, 170);font-family: chaparral-pro, Georgia, serif;display: block;font-style: normal;letter-spacing: normal;margin-top: 0;margin-right: 0;margin-left: 0;text-align: left;">
The 5 best design links, every day.</h2>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- END HEADER \\ --></td>
</tr>
<tr>
<td align="center" valign="top" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<!-- // BEGIN BODY -->
<table border="0" cellpadding="0" cellspacing="0" width="100%"
style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td align="center" valign="top" id="templateBody" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;background-color: #ffffff;border-top: 0;border-bottom: 0;padding: 20px;">
<table border="0" cellpadding="0" cellspacing="0" width="600"
class="templateContainer" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td valign="top" class="bodyContent" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;color: #505050;font-family: Helvetica;font-size: 13px;line-height: 150%;text-align: left;">
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=675cf30f66&e=4fa588f2fc"
class="post-title" target="_blank" style=
"color: #333333;font-weight: normal;text-decoration: none;"><span style="font-size:24px; font-family: georgia, serif; line-height:1.3;">
Help Us Record the Front-End Design Conf Videos</span></a>
<a style="margin-top: -5px;vertical-align: middle;border-radius: 3px;background: #eee;font-size: 10px;text-transform: uppercase;font-weight: regular;color: #aaa;padding: 1px 5px;display: inline-block;text-decoration: none;"
href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=9750bb83aa&e=4fa588f2fc">
Sponsored</a><br />
<a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=eb029eb24a&e=4fa588f2fc"
style=
"color: #a9a9a9;font-weight: normal;text-decoration: none;">www.indiegogo.com</a><br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=76bc52b512&e=4fa588f2fc"
style=
"color: #333333;font-weight: normal;text-decoration: none;"><img src="http://twitter.com/api/users/profile_image/dandenney"
height="20px" width="20px" style=
"vertical-align: middle;border: 0;height: auto;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
</a> Added by <a style=
"font-weight: bold;color: #f36c3d;text-decoration: none;" href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=a28f0f360a&e=4fa588f2fc">
@dandenney</a> <span style="color:#a9a9a9">|</span> <a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=9f046689f0&e=4fa588f2fc"
style="color: #a9a9a9;font-weight: bold;text-decoration: none;"
target="_blank"><img src=
"http://sidebar.io/img/twitter-icon-grey.png" style=
"height: 12px;width: 12px;border: 0;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
Tweet</a><br />
<br />
<br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=cc1961954d&e=4fa588f2fc"
class="post-title" target="_blank" style=
"color: #333333;font-weight: normal;text-decoration: none;"><span style="font-size:24px; font-family: georgia, serif; line-height:1.3;">
iOS 7</span></a><br />
<a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=d7d7251559&e=4fa588f2fc"
style=
"color: #a9a9a9;font-weight: normal;text-decoration: none;">mattgemmell.com</a><br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=0c03fe4e92&e=4fa588f2fc"
style=
"color: #333333;font-weight: normal;text-decoration: none;"><img src="http://twitter.com/api/users/profile_image/leemunroe"
height="20px" width="20px" style=
"vertical-align: middle;border: 0;height: auto;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
</a> Added by <a style=
"font-weight: bold;color: #f36c3d;text-decoration: none;" href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=7bebad29c7&e=4fa588f2fc">
@leemunroe</a> <span style="color:#a9a9a9">|</span> <a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=d6f24f0007&e=4fa588f2fc"
style="color: #a9a9a9;font-weight: bold;text-decoration: none;"
target="_blank"><img src=
"http://sidebar.io/img/twitter-icon-grey.png" style=
"height: 12px;width: 12px;border: 0;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
Tweet</a><br />
<br />
<br />
<a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=69dbcd64c1&e=4fa588f2fc"
class="post-title" target="_blank" style=
"color: #333333;font-weight: normal;text-decoration: none;"><span style="font-size:24px; font-family: georgia, serif; line-height:1.3;">
The Secret Handshake</span></a><br />
<a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=50506839ec&e=4fa588f2fc"
style=
"color: #a9a9a9;font-weight: normal;text-decoration: none;">learnthesecrethandshake.com</a><br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=cc0b484a05&e=4fa588f2fc"
style=
"color: #333333;font-weight: normal;text-decoration: none;"><img src="http://twitter.com/api/users/profile_image/SachaGreif"
height="20px" width="20px" style=
"vertical-align: middle;border: 0;height: auto;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
</a> Added by <a style=
"font-weight: bold;color: #f36c3d;text-decoration: none;" href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=ce4cb421c7&e=4fa588f2fc">
@SachaGreif</a> <span style="color:#a9a9a9">|</span> <a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=62fcb6bfdf&e=4fa588f2fc"
style="color: #a9a9a9;font-weight: bold;text-decoration: none;"
target="_blank"><img src=
"http://sidebar.io/img/twitter-icon-grey.png" style=
"height: 12px;width: 12px;border: 0;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
Tweet</a><br />
<br />
<br />
<a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=aa3c1c59f3&e=4fa588f2fc"
class="post-title" target="_blank" style=
"color: #333333;font-weight: normal;text-decoration: none;"><span style="font-size:24px; font-family: georgia, serif; line-height:1.3;">
Dear NSA, let me take care of your slides.</span></a><br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=cc73a11a9f&e=4fa588f2fc"
style=
"color: #a9a9a9;font-weight: normal;text-decoration: none;">slideshare.net</a><br />
<a href=
"http://sidebar.us2.list-manage2.com/track/click?u=b5af47765edbd2fc173dbf27a&id=ff1a04a456&e=4fa588f2fc"
style=
"color: #333333;font-weight: normal;text-decoration: none;"><img src="http://twitter.com/api/users/profile_image/StarBurst1977"
height="20px" width="20px" style=
"vertical-align: middle;border: 0;height: auto;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
</a> Added by <a style=
"font-weight: bold;color: #f36c3d;text-decoration: none;" href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=e9a5a5d7c6&e=4fa588f2fc">
@StarBurst1977</a> <span style="color:#a9a9a9">|</span> <a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=d399b19267&e=4fa588f2fc"
style="color: #a9a9a9;font-weight: bold;text-decoration: none;"
target="_blank"><img src=
"http://sidebar.io/img/twitter-icon-grey.png" style=
"height: 12px;width: 12px;border: 0;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
Tweet</a><br />
<br />
<br />
<a href=
"http://sidebar.us2.list-manage1.com/track/click?u=b5af47765edbd2fc173dbf27a&id=daeecc8e56&e=4fa588f2fc"
class="post-title" target="_blank" style=
"color: #333333;font-weight: normal;text-decoration: none;"><span style="font-size:24px; font-family: georgia, serif; line-height:1.3;">
How award-winning work can make or break you</span></a><br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=576c592751&e=4fa588f2fc"
style=
"color: #a9a9a9;font-weight: normal;text-decoration: none;">blog.f-i.com</a><br />
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=8ee8c5f303&e=4fa588f2fc"
style=
"color: #333333;font-weight: normal;text-decoration: none;"><img src="http://twitter.com/api/users/profile_image/allfordesign"
height="20px" width="20px" style=
"vertical-align: middle;border: 0;height: auto;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
</a> Added by <a style=
"font-weight: bold;color: #f36c3d;text-decoration: none;" href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=f2c7ff1a5f&e=4fa588f2fc">
@allfordesign</a> <span style="color:#a9a9a9">|</span> <a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=b2af1fdd83&e=4fa588f2fc"
style="color: #a9a9a9;font-weight: bold;text-decoration: none;"
target="_blank"><img src=
"http://sidebar.io/img/twitter-icon-grey.png" style=
"height: 12px;width: 12px;border: 0;line-height: 100%;outline: none;text-decoration: none;display: inline;max-width: 600px;" />
Tweet</a><br />
<br />
<br /></td>
</tr>
</table>
</td>
</tr>
</table>
<!-- END BODY \\ --></td>
</tr>
<tr>
<td align="center" valign="top" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<!-- // BEGIN FOOTER -->
<table border="0" cellpadding="0" cellspacing="0" width="100%"
style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td align="center" valign="top" id="templateFooter" style=
"padding-bottom: 40px;border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;border-top: 0;padding: 20px;">
<table border="0" cellpadding="0" cellspacing="0" width="600"
class="templateContainer" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;">
<tr>
<td colspan="3" valign="top" class="footerContent" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;color: #808080;font-family: Helvetica;font-size: 10px;line-height: 150%;text-align: left;">
<a href=
"http://sidebar.us2.list-manage.com/track/click?u=b5af47765edbd2fc173dbf27a&id=e281d1415a&e=4fa588f2fc"
style=
"color: #606060;font-weight: normal;text-decoration: underline;">Follow
on Twitter</a> <a href=
"http://us2.forward-to-friend.com/forward?u=b5af47765edbd2fc173dbf27a&id=c214e283a3&e=4fa588f2fc"
style=
"color: #606060;font-weight: normal;text-decoration: underline;">Forward
to Friend</a> <a href=
"http://us2.campaign-archive2.com/?u=b5af47765edbd2fc173dbf27a&id=c214e283a3&e=4fa588f2fc"
target="_blank" style=
"color: #606060;font-weight: normal;text-decoration: underline;">View
in Browser</a></td>
</tr>
<tr>
<td valign="top" class="footerContent" style=
"padding-top: 20px;padding-bottom: 20px;border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;color: #808080;font-family: Helvetica;font-size: 10px;line-height: 150%;text-align: left;">
<em>Copyright © 2013 Sacha Greif, All rights reserved.</em><br />
You're receiving this because you signed up on
http://sidebar.io<br />
<br />
<strong>Our mailing address is:</strong><br />
<div class="vcard"><span class="org fn">Sacha Greif</span>
<div class="adr">
<div class="street-address">---</div>
<span class="locality">Paris</span> <span class=
"postal-code">-</span>
<div class="country-name">France</div>
<br />
<a href=
"http://sidebar.us2.list-manage2.com/vcard?u=b5af47765edbd2fc173dbf27a&id=9abeab36bb"
class="hcard-download">Add us to your address book</a></div>
<br />
<br /></div>
</td>
</tr>
<tr>
<td colspan="3" valign="top" class="footerContent" style=
"border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;color: #808080;font-family: Helvetica;font-size: 10px;line-height: 150%;text-align: left;">
<a href=
"http://sidebar.us2.list-manage.com/unsubscribe?u=b5af47765edbd2fc173dbf27a&id=9abeab36bb&e=4fa588f2fc&c=c214e283a3"
style=
"color: #606060;font-weight: normal;text-decoration: underline;">unsubscribe
from this list</a> <a href=
"http://sidebar.us2.list-manage.com/profile?u=b5af47765edbd2fc173dbf27a&id=9abeab36bb&e=4fa588f2fc"
style=
"color: #606060;font-weight: normal;text-decoration: underline;">update
subscription preferences</a> </td>
</tr>
</table>
</td>
</tr>
</table>
<!-- END FOOTER \\ --></td>
</tr>
</table>
<!-- END TEMPLATE SECTIONS \\ --></td>
</tr>
</table>
</center>
<img src=
"http://sidebar.us2.list-manage.com/track/open.php?u=b5af47765edbd2fc173dbf27a&id=c214e283a3&e=4fa588f2fc"
height="1" width="1" />
</body>
</html>
</div>
再次,Apple 提供了以下预览:
每天有 5 个最佳设计链接,由精选的优秀编辑策划。
我一直在尝试使用 HTMLParser 来解析它,但是每封电子邮件都是不同的,所以这并不容易实现。