开发者

UIWebView loading from Sqlite3

开发者 https://www.devze.com 2023-02-19 05:33 出处:网络
I have an NSString variable \'body\' that I am populating from sqlite3. The string is formatted with \'\\n\' and \'\\t\' such that when printed in NSLog it looks like

I have an NSString variable 'body' that I am populating from sqlite3. The string is formatted with '\n' and '\t' such that when printed in NSLog it looks like

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 

I am trying to load this string into a UIWebView and still preserve all the formatting for which I browsed and found out this.

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];  
       }

But I see in the UIWebView the formatting is not preserved and it turns up like this:

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 

Can anyone come to my rescue?


maybe you should add the p tag around your string and a br at the end of each line:

<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>

you should use something like this before encoding in UTF8:

body = [NSString stringWithFormat:@"<p>%@</p>", body];
body = [body stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>\n"];
0

精彩评论

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