Search code examples
swiftwebviewline-breaks

Line breaks recognition in swift


I've made an app that would call data from Database via webservice, create an XML page and parse it. After that it would post a text in WebView but for some reason it can't recognise line breaks and just post the text ignoring all "\n".

Is there something I've missed?

Thanks!

This is how I add text to my WebView:

import UIKit


class PubViewController: UIViewController {


@IBOutlet weak var myWebView1: UIWebView!

var selectedFeedTitle = String()
var selectedFeedFeedContent = String()
var selectedFeedURL = String()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let feedContent:String! = "<h3>\(selectedFeedTitle)</h3>\(selectedFeedFeedContent)"
    myWebView1.loadHTMLString(feedContent, baseURL: nil)
}

This is what lies in "feedcontent"


Solution

  • Line breaks are ignored in HTML. You have to use the <br/> tag:

    let feedContent:String! = "<h3>\(selectedFeedTitle)</h3>\(selectedFeedFeedContent)"
            .stringByReplacingOccurrencesOfString("\n", withString: "<br/>")