Currently, i've reached the place where i succeeded in parsing the local JSON data into SwiftUI's list-view. Now, the data i want to show to the user is formatted in html with various html tags combined with the main description. I want to show that description into WKWebView or some other view where i can show exactly html formatted text with html properties applied.
Below is the current scenario where i'm stuck.
when user presses the last drop down list, i want them to show that description on other view which can read and apply all the html formatted properties of the JSON data instead of just showing
and other html tags.
Below is the code that i'm currently using..
import SwiftUI
struct lvl4: View {
@State var book: Book = Book()
var body: some View {
NavigationView {
// alternatively
List {
ForEach(book.bookContent) { bookContent in
Section(header: Text(bookContent.title)) {
OutlineGroup(bookContent.child, children: \.child) { item in
Text(item.title)
.fontWeight(.ultraLight)
.lineSpacing(20)
}
}
}
}
ForEach(book.bookContent) { bookContent in
VStack {
Text(bookContent.title)
.foregroundColor(Color.black)
.fontWeight(.heavy)
List(bookContent.child, children: \.child) { item in
Text(item.title)
.padding()
.monospacedDigit()
.drawingGroup()
}
}
}
}.navigationViewStyle(.stack)
.onAppear {
loadData()
}
}
// func replacingOccurrences(of target: String = "<p>",
// with replacement: String = "",
// options: NSString.CompareOptions = [],
// range searchRange: NSRange) -> String{
//
// return replacement
// }
func loadData() {
do {
if let url = Bundle.main.url(forResource: "સાગર મંથન", withExtension: "json") {
let data = try Data(contentsOf: url)
book = try JSONDecoder().decode(Book.self, from: data)
}
} catch {
print("error: \(error)")
}
}
struct Book: Identifiable, Codable {
let id = UUID()
var bookTitle: String = ""
var isLive: Bool = false
var userCanCopy: Bool = false
var bookContent: [BookContent] = []
enum CodingKeys: String, CodingKey {
case bookTitle = "book_title"
case isLive = "is_live"
case userCanCopy = "user_can_copy"
case bookContent = "book_content"
}
}
struct BookContent: Identifiable, Codable {
let id = UUID()
var title, type: String
var child: [Child]
// var rendered: String
}
struct Child: Identifiable, Codable {
let id = UUID()
var title, type: String
var child: [Child]?
}
}
struct lvl4_Previews: PreviewProvider {
static var previews: some View {
lvl4()
}
}
New updated code with implemented disclosuregroup, outline group, content view: that has the attributed string working and implemented disclosure group
import SwiftUI
import Foundation
struct ContentView: View {
@EnvironmentObject var booksList:BooksList
@State var books: [BookModel] = []
@State var selection: BookModel?
var body: some View {
// NavigationView {
VStack(alignment:.trailing, spacing: 40 ){
ScrollView(.vertical, showsIndicators: false){
ForEach(booksList.books) { book in
// NavigationLink(destination: lvl4(books: [book], selection: nil)){
// Text(book.bukTitle!)
// .listRowInsets(EdgeInsets())
//
if #available(iOS 15.0, *) {
DisclosureGroup ("\(Text(book.bukTitle!) .fontWeight(.medium) .font(.system(size: 27))) "){
ForEach(book.bookContent ?? []) { bookContent in
DisclosureGroup("\(Text(bookContent.title).fontWeight(.light) .font(.system(size: 25)))")
{
OutlineGroup(bookContent.child , children: \.child) { item in
if #available(iOS 15, *) {
Text(attributedString(from: item.title, font: Font.system(size: 23) ))
.navigationTitle(Text(bookContent.title))
.padding(10)
// if (([Child].self as? NSNull) == nil) {
// NavigationLink(destination: ScrollView {Text(attributedString(from: item.title, font: Font.system(size: 25) )).padding(30) .lineSpacing(10) .navigationTitle(Text(bookContent.title)) .navigationBarTitleDisplayMode(.inline)
//
// })
// {
//
// // EmptyView()
// // .navigationTitle(Text(bookContent.title))
// }
// }
}
}
}
}
}
}
}
}
}.padding(35)
}
//
// DisclosureGroup("\(Text(book.bukTitle!).fontWeight(.light) .font(.system(size: 23)))"){
//
// ForEach(book.bookContent ?? []) { bookContent in
//
// DisclosureGroup("\(Text(bookContent.title))" ){
//
// OutlineGroup(bookContent.child, children: \.child) { chld in
//
//
// List(bookContent.child, children: \.child)
// {
// OutlineGroup(bookContent.child, children: \.child) { item in
// if #available(iOS 15, *) {
//
// NavigationLink(destination: ScrollView{Text(attributedString(from: item.title, font: Font.system(size: 22) )).padding(30) .lineSpacing(10) .navigationTitle(Text(bookContent.title)) .navigationBarTitleDisplayMode(.inline)}){
// EmptyView()
//
// }
// }
// }
// }
// }
// }
// }
// }
// }
// }
// }
//}
@available(iOS 13.0.0, *)
struct ContentView_Previews: PreviewProvider {
@available(iOS 13.0.0, *)
static var previews: some View {
ContentView()
}
}
}
Issue 1 with implemented Navigationlink
: The updated code has Navigationlink commented out as the code is acting unusual..: The navigationLink starts to show in all the nested branches after first drop down. Below shown Picture dipcts the issue. ::