gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:10:59 26 4
gpt4 key购买 nike

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

最佳答案

我建议两件事:1)将 case "div": 添加到您的 select 语句中。输入类似:

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

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

关于swift - 将 html 转换为文本(空格问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52177737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com