Last active
October 16, 2023 21:22
-
-
Save kevinjbayer/bccda38e3a37858a11f7b4a73d54944f to your computer and use it in GitHub Desktop.
Render HTML strings in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// Animations | |
// | |
// Created by Kevin Bayer on 4/5/21. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
var htmlText = "<span style=\"font-size:20px\"><a href=\"http://www.theafghanwhigs.com\">The Afghan Whigs</a> reign supreme</span>" | |
var body: some View { | |
VStack { | |
Link("Stackoverflow", destination: URL(string: "https://stackoverflow.com")!) | |
Text(htmlText) | |
.padding() | |
TextCustom(html: htmlText) | |
.padding() | |
HTMLText(html: htmlText) | |
.padding() | |
} | |
} | |
} | |
// Using UITextView | |
struct TextCustom: UIViewRepresentable { | |
let html: String | |
func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) { | |
DispatchQueue.main.async { | |
let data = Data(self.html.utf8) | |
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, ], documentAttributes: nil) { | |
uiView.isEditable = false | |
uiView.attributedText = attributedString | |
} | |
} | |
} | |
func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView { | |
let label = UITextView() | |
return label | |
} | |
} | |
// Using UILabel | |
struct HTMLText: UIViewRepresentable { | |
let html: String | |
func makeUIView(context: UIViewRepresentableContext<Self>) -> UILabel { | |
let label = UILabel() | |
DispatchQueue.main.async { | |
let data = Data(self.html.utf8) | |
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) { | |
label.attributedText = attributedString | |
} | |
} | |
return label | |
} | |
func updateUIView(_ uiView: UILabel, context: Context) {} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
func makeUIView(context: Context) -> UITextView {
let uiTextView = UITextView()
uiTextView.isEditable = false
uiTextView.isScrollEnabled = false
uiTextView.clipsToBounds = true
uiTextView.textContainer.lineBreakMode = .byWordWrapping
uiTextView.textContainer.widthTracksTextView = true
uiTextView.setContentHuggingPriority(.defaultHigh, for: .vertical)
uiTextView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
uiTextView.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
uiTextView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return uiTextView
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I have a problem when I call TextCustom inside ScrollView it does not show anything. any suggestion?