mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +00:00
57 lines
1.6 KiB
Swift
57 lines
1.6 KiB
Swift
//
|
|
// FormattedBodyText.swift
|
|
// ElementX
|
|
//
|
|
// Created by Stefan Ceriu on 24/03/2022.
|
|
// Copyright © 2022 Element. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
struct FormattedBodyText: View {
|
|
let attributedComponents: [AttributedStringBuilderComponent]
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 0.0) {
|
|
ForEach(attributedComponents, id: \.self) { component in
|
|
if component.isBlockquote {
|
|
HStack(spacing: 4.0) {
|
|
Rectangle()
|
|
.foregroundColor(Color.red)
|
|
.frame(width: 4.0)
|
|
Text(component.attributedString)
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
} else {
|
|
Text(component.attributedString)
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
}
|
|
}
|
|
.tint(.elementGreen)
|
|
}
|
|
}
|
|
|
|
struct FormattedBodyText_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
let htmlString = """
|
|
Text before blockquote
|
|
<blockquote>
|
|
<b>bold</b> <i>italic</i>
|
|
</blockquote>
|
|
Text after blockquote
|
|
"""
|
|
|
|
let attributedStringBuilder = AttributedStringBuilder()
|
|
let attributedString = attributedStringBuilder.fromHTML(htmlString)
|
|
|
|
if let components = attributedStringBuilder.blockquoteCoalescedComponentsFrom(attributedString) {
|
|
VStack {
|
|
FormattedBodyText(attributedComponents: components)
|
|
.fixedSize()
|
|
}
|
|
}
|
|
}
|
|
}
|