1
Fork 0

Add support for rendering html post content

This commit is contained in:
redstrate 2020-06-04 06:35:41 -04:00
parent 2bf082f2f9
commit c42537d885

View file

@ -7,6 +7,37 @@ extension String {
} }
} }
/// A custom view to use NSAttributedString in SwiftUI
struct AttributedText: UIViewRepresentable {
func updateUIView(_ uiView: UILabel, context: Context) {
}
let html: String
init(_ html: String) {
self.html = html
}
public func makeUIView(context: UIViewRepresentableContext<AttributedText>) -> UILabel {
let textView = UILabel()
textView.backgroundColor = .clear
textView.lineBreakMode = .byWordWrapping
textView.numberOfLines = 0
textView.lineBreakMode = .byWordWrapping
textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
DispatchQueue.main.async {
let data = Data(self.html.utf8)
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
textView.attributedText = attributedString
}
}
return textView
}
}
struct PostView: View { struct PostView: View {
let post: Post let post: Post
@ -53,7 +84,7 @@ struct PostView: View {
} }
} }
Text(post.getContent()) AttributedText(post.getContent()).frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
} }
}.frame(maxWidth: .infinity) }.frame(maxWidth: .infinity)
} }