我有一个NSString变量'body',我从sqlite3填充。 该字符串使用'\ n'和'\ t'格式化,以便在NSLog中打印时,它看起来像
gagan
On 3/21/11 5:36 PM,
From:User91@SMODEVGWS03.com
To:"User91 Test"<User91@SMODEVGWS03.com>
Subject: test it
wrote:
ravan
~~~~~~~~~~~~~~~~
Attachment not downloaded due to unsupported type
我正在尝试将此字符串加载到UIWebView中,并且仍然保留我浏览过的所有格式,并发现了这一点。
NSString *body contains the above string with all the formatting.
NSData* data=[body dataUsingEncoding:NSUTF8StringEncoding];
if (data) {
[webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];
}
但是我在UIWebView中看到格式没有被保留,它就像这样:
gagan On 3/21/11 5:36 PM,
From:User91@SMODEVGWS03.com
To:"User91 Test"<User91@SMODEVGWS03.com> Subject: test it wrote: ravan
~~~~~~~~~~~~~~~~Attachment not
downloaded due to unsupported type
有人可以救我吗?
答案 0 :(得分:0)
也许您应该在字符串周围添加 p 标记,并在每行的末尾添加br:
<p>
gagan<br/>
<br/>
On 3/21/11 5:36 PM,<br/>
From:User91@SMODEVGWS03.com<br/>
To:"User91 Test"<User91@SMODEVGWS03.com><br/>
Subject: test it<br/>
wrote:<br/>
<br/>
ravan<br/>
<br/>
<br/>
~~~~~~~~~~~~~~~~<br/>
Attachment not downloaded due to unsupported type<br/>
</p>
在使用UTF8进行编码之前,你应该使用类似的东西:
body = [NSString stringWithFormat:@"<p>%@</p>", body];
body = [body stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>\n"];