i am working on an application where I am fetching data from the endpoint and then showing them to the user. Most of the "String" data that I am getting is in html format, so I need to convert it into the String. Everything is working, except removing the trailing new line character. These are the extensions that I am using:
Data extension:
extension Data {
var htmlToAttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error:", error)
return nil
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
String extension:
extension String {
var htmlToAttributedString: NSAttributedString? {
return Data(utf8).htmlToAttributedString
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
Code sample:
let descriptionHtml = "\n<p>Rømø beach is still a young beach and created from blown sand so fine that it resembles tiny diamonds. All year round, from morning to evening, there will be activity on the beach, known as one of Northern Europe’s absolute best and widest.</p>\n"
let descriptionAsString = descriptionHtml.htmlToString
//Expected result: "Rømø beach is still a young beach and created from blown sand so fine that it resembles tiny diamonds. All year round, from morning to evening, there will be activity on the beach, known as one of Northern Europe’s absolute best and widest."
// Actual result: "Rømø beach is still a young beach and created from blown sand so fine that it resembles tiny diamonds. All year round, from morning to evening, there will be activity on the beach, known as one of Northern Europe’s absolute best and widest.\n"
//
I can simply fix this problem by replacing "\n"
with ""
but i doesn't seem like a gentle solution and since I am using the actual NSAttributedString initializer it surprised my that is not working as I expected it to work. Is there any reason why it leaves the new line character at the end, and is there any way to fix it without manually replacing the new line character?
This text is wrapped in a <p>
tag. Typically that would end in a newline if it were rendered. This should be expected behavior. If you don't want this to be "paragraph-like" then it shouldn't be in a <p>
tag.
I you remove the <p></p>
, but leave the final \n
, it will be rendered with a trailing space, which is the normal way of rendering line breaks in HTML. Again, this should be expected behavior. You're rendering HTML. This allows you to render HTML in chunks and glue together the resulting Strings in a sensible way.