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

问题描述 投票:0回答:1

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


        }

这是我转换的HTML

    <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> 
swift swift4 string-formatting stringbuilder
1个回答
0
投票

我建议两件事:1)在你的select语句中添加case“div”:像这样的东西:

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

2)使用相同类型的代码修改默认情况,测试空格字符。

© www.soinside.com 2019 - 2024. All rights reserved.