如何在swiftui 14中读取json文件的格式化文本



我有一个json文件,其中包含html样式的文本:我需要维护包含的文本格式:粗体、斜体、下划线等。

我正在使用SwiftUI和Xcode 14。我将json读取到SwiftUI列表中,然后导航到显示描述文本的详细页面。我找不到任何关于如何维护json文件中格式化文本的示例。我在谷歌上搜索过,看了苹果开发者的文档等等,但都无济于事。有人能帮我吗。我的应用程序依赖于格式正确的文本。苹果不包括某些东西,这似乎很奇怪。这在html和javascript中很容易。我错过了什么?我不是专业程序员,刚开始使用SwiftUI。TIA-

[                               
{ "imageurl": "1.png",
"levelLongDesc":"A longer description",
"id": "1.",
"name": "A name)",
"page": "Details",
"description":"<p><b>E pluribus unum</b></p><b>Instructions.</b> Latin for “Out of many one”, is a motto requested by <i>Pierre Eugene du Simitiere</i> (originally Pierre-Eugène Ducimetière) and found in 1776 on the Seal of the United States, along with Annuit cœptis and Novus ordo seclorum, and adopted by an Act of Congress in 1782.</p><p>",
"videoDemo":"myvideo"
}...]
import SwiftUI

struct LandmarkDetail:视图{var地标:地标@状态var Description=AttributedString("(var body:some视图{

NavigationView{
ScrollView {
RectImage(image: landmark.image)

.padding(.top, 30)

.font(.subheadline)
.foregroundColor(.secondary)

Divider()

Text("(landmark.name)")


Text(landmark.description)
.font(.title3)
.padding(.horizontal, 20)
.onAppear {
(addStyling(landmark.description))
}
} .padding()

}.navigationTitle(landmark.name)
.navigationBarTitleDisplayMode(.inline)

}
private func addStyling(_ htmlString: String) -> NSAttributedString {
var resultString = NSAttributedString()
var data = Data()
// Add the html data/ var in which you stored it
data.append(Data(landmark.description.utf8))
// Convert the attributed String
do {
let attributedString = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
resultString = attributedString
} catch {
print(error)
}
return resultString
}
}

上面是我的详细视图。我不知道在哪里/如何添加这些属性。我尝试的每件事都会出错。再次感谢你们两位尽力提供帮助的人-R

假设解码对象模型的"description"属性是一些简单的html文本,可以使用AttributedStringNSAttributedString,如图所示在此示例中,代码为:

struct ContentView: View {
@State var description = AttributedString("")

var body: some View {
Text(description)
.onAppear {
let txt = """
<p><b>E pluribus unum</b></p><b>Instructions.</b> Latin for “Out of many one”, is a motto requested by <i>Pierre Eugene du Simitiere</i> (originally Pierre-Eugène Ducimetière) and found in 1776 on the Seal of the United States, along with Annuit cœptis and Novus ordo seclorum, and adopted by an Act of Congress in 1782.</p><p>
"""
description = asAttribTxt(txt)
}
}

func asAttribTxt(_ txt: String) -> AttributedString {
let data = txt.data(using: .utf16)!  // <-- adjust to your needs
do {
let nsString = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
return AttributedString(nsString)
} catch {
print(error)
}
return AttributedString(txt) 
}
}
private func addStyling(_ htmlString: String) -> NSAttributedString {
var resultString = NSAttributedString()
var data = Data()
// Add the html data/ var in which you stored it
data.append(Data(htmlString.utf8))
// Convert the attributed String
do {
let attributedString = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
resultString = attributedString
} catch {
print(error)
}
return resultString
}

最新更新