Add support for rendering html post content
This commit is contained in:
parent
2bf082f2f9
commit
c42537d885
1 changed files with 32 additions and 1 deletions
|
@ -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 {
|
||||
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)
|
||||
}
|
||||
|
|
Reference in a new issue