Я преобразовал данные HTML в текст, и у меня возникла проблема с пробелами в начале первого абзаца или заголовка, как показано здесь пробелы перед заголовком пробелы перед абзацем
Я использую 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>