Hi. I am not sure why i get the error message AttributeGraph: cycle detected through attribute
but this does happen i noticed if i have a Text component render html.
Here is the code
import SwiftUI
struct ContentView: View {
@State private var isShowingAlertSheet = false
@State private var alertTitle: String?
@State private var alertDescription: String?
var body: some View {
VStack {
Button(
"click me", action: {
alertTitle = "TITLE HERE"
alertDescription = "<b>hey</b>"
isShowingAlertSheet = true
}
)
}
.sheet(isPresented: $isShowingAlertSheet) {
DetailView(alertTitle: $alertTitle, alertDescription: $alertDescription)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct DetailView: View {
@Binding var alertTitle: String?
@Binding var alertDescription: String?
var body: some View {
VStack {
Text(alertTitle ?? "")
.font(.system(size: 20))
.padding(.top, 10)
.padding(.bottom, 0)
.presentationDetents([.height(250)])
Text(fixTextSize(alertDescription?.htmlMutableAttributedString))
.frame(maxWidth: .infinity)
.padding(.top, 10)
Spacer()
}
.padding()
}
func fixTextSize(_ mutableAttributedString: NSMutableAttributedString?) -> AttributedString {
if let mutableAttributedString {
mutableAttributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 16), range: NSMakeRange(0, mutableAttributedString.length))
return AttributedString(mutableAttributedString)
}
return AttributedString()
}
}
extension String {
var htmlMutableAttributedString: NSMutableAttributedString? {
if let attributedString = try? NSMutableAttributedString(data: Data(self.utf8), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(.black), range: NSMakeRange(0, attributedString.length))
return attributedString
}
return nil
}
}
Thoughts?