When the htmlString size is not so big then it’s working. If size increased then app get crushed. Here is the code snipped. Code actually formate the hlml string and detected arabic text. Applied different font for arabic and other text. I am using “SwiftSoup” library for detect html tags.
struct PrayerLearningTopicByCategoryDetailsCard: View {
@StateObject var prayerTopicByCategoryVM: PrayerLearningTopicByCategoryVM = PrayerLearningTopicByCategoryVM(prayerLearningAPIService: PrayerLearningAPIService())
@EnvironmentObject var nav: NavigationStateManager
@State var categoryName: String
@State var categoryId: Int
@State var isLoading: Bool = false
var body: some View {
VStack {
if isLoading {
LoaderView(isLoading: $isLoading)
} else {
ScrollView {
if let prayerTopicByCategoryData = prayerTopicByCategoryVM.prayerTopicByCategoryData {
VStack {
ForEach(prayerTopicByCategoryData) { data in
if let itemDetails = data.details, let title = data.title, itemDetails.count > 0 {
PrayerTopicByCategoryDetailsView(title: title, details: itemDetails)
}
}
}
.clipShape(RoundedRectangle(cornerRadius: 16))
.padding(.horizontal, 16)
.shadow(radius: 0.8)
}
}
.scrollIndicators(.hidden)
}
}
.padding(.top, 12)
.onAppear(perform: {
isLoading = true
Task {
await prayerTopicByCategory(categoryId: categoryId)
isLoading = false
}
})
}
func prayerTopicByCategory(categoryId: Int) async {
await prayerTopicByCategoryVM.getPrayerTopicbyCategory(payload: PrayerLearningByCategoryPayload(language: AppData.shared.apiLanguage, category: categoryId))
}
}
struct PrayerTopicByCategoryDetailsView: View {
let title: String
let details: [PrayerTopicByCategoryDataDetail]
var body: some View {
VStack {
ForEach(details) { item in
if let textInArabic = item.text {
DemoHtmlTextView(htmlString: textInArabic)
}
}
}
}
}
struct DemoHtmlTextView: View {
@State var htmlString: String
@State var attributedText: Text = Text("")
@State var isFirstListItem: Bool = true
var body: some View {
VStack {
attributedText
.frame(maxWidth: .infinity, alignment: .topLeading)
}
.onAppear {
DispatchQueue.main.async {
self.extractAndClassifyText(from: htmlString)
}
}
}
func extractAndClassifyText(from html: String) {
guard !html.isEmpty else {
print("HTML string is empty")
return
}
if !html.contains("<") || !html.contains(">") {
DispatchQueue.main.async {
attributedText = Text(html)
.foregroundColor(.deenBlack)
.font(.deenTextFontSemiboldBN)
}
return
}
do {
let document = try SwiftSoup.parse(html)
guard let body = try document.select("body").first() else {
print("No body found in the HTML document")
return
}
var composedText = Text("")
let elements = body.children()
for (index, element) in elements.enumerated() {
let tagName = element.tagName()
if tagName == "p" {
let paragraphText = try element.text().trimmingCharacters(in: .whitespacesAndNewlines)
composedText = addParagraph(paragraphText, isFirstElement: index == 0, composedText: composedText, element: element)
}
if tagName == "ul" {
let listItems = try element.select("li")
for listItem in listItems {
let listItemText = try listItem.text().trimmingCharacters(in: .whitespacesAndNewlines)
composedText = addListItem(listItemText, composedText: composedText, element: element)
}
}
}
DispatchQueue.main.async {
attributedText = composedText
}
} catch {
print("Error parsing HTML or extracting text: (error.localizedDescription)")
}
}
func addParagraph(_ text: String, isFirstElement: Bool, composedText: Text, element: Element) -> Text {
var updatedText = composedText
if !isFirstElement {
updatedText = updatedText + Text("n")
}
let words = text.split(separator: " ")
for (wordIndex, word) in words.enumerated() {
let isBold = (try? element.select("strong").contains(where: { try $0.text().contains(String(word)) })) ?? false
let isArabic = isArabicText(String(word))
let styledWord = Text(String(word))
.foregroundColor(.deenBlack)
.font(isArabic ? Font.custom("AlQuran IndoPak by QuranWBW", size: 22) : .deenTextFontSemiboldBN)
.fontWeight(isBold ? .bold : .regular)
updatedText = updatedText + styledWord
if wordIndex < words.count - 1 {
updatedText = updatedText + Text(" ")
}
}
return updatedText
}
func addListItem(_ text: String, composedText: Text, element: Element) -> Text {
var updatedText = composedText
// if isFirstListItem {
// updatedText = updatedText + Text("n")
// }
isFirstListItem = false
updatedText = updatedText + Text("• ").font(.headline)
let words = text.split(separator: " ")
for (wordIndex, word) in words.enumerated() {
let isBold = (try? element.select("strong").contains(where: { try $0.text().contains(String(word)) })) ?? false
let isArabic = isArabicText(String(word))
let styledWord = Text(String(word))
.foregroundColor(.deenBlack)
.font(isArabic ? Font.custom("AlQuran IndoPak by QuranWBW", size: 22) : .deenTextFontSemiboldBN)
.fontWeight(isBold ? .bold : .regular)
updatedText = updatedText + styledWord
if wordIndex < words.count - 1 {
updatedText = updatedText + Text(" ")
}
}
updatedText = updatedText + Text("n")
return updatedText
}
func isArabicText(_ text: String) -> Bool {
let arabicRange = text.range(of: "[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]", options: .regularExpression)
return arabicRange != nil
}
}
In console getting “AttributeGraph: cycle detected through attribute” message and code get crushed. But when html string size is small then it’s working fine