2

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&#8217;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?

2

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I can't remove the tag, since I am fetching the data from the endpoint, the sample code i provided is just a hardcoded presentation of the problem.
@schemabuoi so just deal with the resulting string descriptionHtml.htmlToString.filter{!$0.isWhitespace}. if your intent is to remove only the whitespaces at the end of the string descriptionHtml.htmlToString.trimmingCharacters(in: .whitespaces)
Agreed with Leo. The renderer is doing the right thing. If you don't want that, you should post-process the string.
@LeoDabus none of your solutions are working. First one removes spaces between words and the second one just doesn't work. But it doesn't matter, if I want the trivial solution I am going to use the method I mentioned in the comment under my question, I was just searching for something "more gentle" then simple string manipulation
@schemabuoi sorry posted the wrong property descriptionHtml.htmlToString.filter{!$0.isNewline} or descriptionHtml.htmlToString.trimmingCharacters(in: .newlines)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.