0

I converted HTML data to text and I am having a problem with white spaces at the beginning of the first paragraph or header as shown here white spaces before header white spaces before paragraph

I am using AttributedStringBuilder to help me format the text and they don't support trim white spaces

here is my code

if let post = post{
        if  let postContent = postContent {   // may be called before outlets are set
            let builder = AttributedStringBuilder()
            let html = post.content
            builder.text(("\(post.title)\n\n"), attributes: [.font(UIFont.init(name: "Andada SC", size: 32)!)])
            do {
                guard let doc: Document = try? SwiftSoup.parse(html) else { return }

                let elements = try doc.getAllElements()
                for element in elements {
                    for textNode in element.textNodes() {
                        print(textNode.text())
                        switch element.tagName() {
                        case "h1" :
                            builder.text(("\(textNode.text())\n\n"), attributes: [.font(UIFont.init(name: "Andada SC", size: 28)!) ])
                        case "h2" :
                            builder.text(("\(textNode.text())\n\n"), attributes: [.font(UIFont.init(name: "Andada", size: 24)!)  ])
                        case "p" :
                            builder.text(("\(textNode.text())\n\n"), attributes: [.font(UIFont.init(name: "Andada", size: 16)!), .alignment(.left), .paragraphStyle(NSParagraphStyle.default) ])
                        case "blockquote" :
                            builder.text(("\(textNode.text())\n\n"), attributes: [.font(UIFont.init(name: "Andada", size: 17)!) ])
                        default:
                            builder.text(("\(textNode.text())"), attributes: [.font(UIFont.init(name: "Andada", size: 16)!)])
                        }

                    }
                }
                postContent.attributedText = builder.attributedString

            } catch {
                print(error)
            }


        }

and this is the html that I converted

    <div class="elementor elementor-2402">
        <div class="elementor-inner">
            <div class="elementor-section-wrap">
                        <section data-id="aae538a" class="elementor-element elementor-element-aae538a elementor-section-boxed elementor-section-height-default elementor-section-height-default elementor-section elementor-top-section" data-element_type="section">
                    <div class="elementor-container elementor-column-gap-default">
            <div class="elementor-row">
            <div data-id="7e70f5d" class="elementor-element elementor-element-7e70f5d elementor-column elementor-col-100 elementor-top-column" data-element_type="column">
        <div class="elementor-column-wrap elementor-element-populated">
                <div class="elementor-widget-wrap">
            <div data-id="27ae282" class="elementor-element elementor-element-27ae282 elementor-widget elementor-widget-text-editor" data-element_type="text-editor.default">
            <div class="elementor-widget-container">
                <div class="elementor-text-editor elementor-clearfix"><h2>Lorem ipsum dolor sit er</h2><p>elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</p><h2>Lorem ipsum dolor sit</h2><p>er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</p></div>
            </div>
            </div>
                    </div>
        </div>
    </div>
                    </div>
        </div>
    </section>
                    </div>
        </div>
    </div> 
Nayef
  • 33
  • 4
  • What's the html you're trying to convert? – Cristik Sep 05 '18 at 05:50
  • See above Please – Nayef Sep 05 '18 at 06:31
  • I ran the above code without the string builder library. I don't see any extra white spaces in the

    ,

    , or

    tags. The default case will catch all of your
    , and some of those will be "\n ". What AttributedStringBuilder library are you using? Note that you can always inspect and trim the string first.

    – Bill Sep 06 '18 at 23:49

1 Answers1

0

I suggest two things: 1) add case "div": to your select statement. Put something like:

if (textNode.text() != " ") {
    builder.text(("\(textNode.text())"), attributes: [.font(UIFont.init(name: 
    "Andada", size: 16)!)])
}

2) Modify the default case with the same sort of code, testing for the space character.

Bill
  • 1,588
  • 12
  • 21