将html转换为文本(空格问题)

时间:2018-09-05 05:35:23

标签: swift swift4 string-formatting stringbuilder

我将HTML数据转换为文本,并且在第一段或标题的开头出现空格问题,如下所示:white spaces before header white spaces before paragraph

我正在使用AttributedStringBuilder帮助我设置文本格式,并且它们不支持修剪空白

这是我的代码

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)
            }


        }

这是我转换的

    <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> 

1 个答案:

答案 0 :(得分:0)

我建议两件事:1)在您的select语句中添加大小写“ div”:像这样:

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

2)用相同类型的代码修改默认大小写,以测试空格字符。