How to save / display line breaks in UIWebView for iPhone?

I have a UIWebView-based iPhone application that reads in HTML from a SQLite database. The user can save new information entered through a UITextView that accepts a carriage return. How to correctly display these carriage returns (line breaks) in a UIWebView? I tried using something like this:

NSString *HTMLData = [mySQLiteDataObject text]; HTMLData = [HTMLData stringByReplacingOccurrencesOfString:@"\\n" withString:@"<br>"]; [webView loadHTMLString:HTMLData baseURL:nil]; 

But this does not look like the text is recognized as having line breaks: the text is displayed in one continuous line in the webView. When I print text in the console, it goes through line breaks.

I tried using \ n, \ r and \ r \ n in the above code example, without success. Am I storing user text incorrectly or doing the wrong thing on the display side?

+4
source share
2 answers

You can see two backslashes in your search bar: you must replace "\n" and not "\\n" for it to work.

But you really want to sanitize your data before loading it into UIWebView. At least you should mask <,>, &, and. "

+2
source

If I'm not mistaken, replacing "\\n" with an HTML break tag, it should look like this:

  HTMLData = [HTMLData stringByReplacingOccurrencesOfString:@"\\n" withString:@"<br />"]; 

It seems you are missing the slash in the break statement.

0
source

All Articles