如何自定义属性字符串以与 SwiftUi 和 JSON 一起使用

问题描述 投票:0回答:1

我的 SwiftUI 应用程序正在将一些 JSON 解码为对象(Landmark),然后将文本转换为 AttributedString 以显示粗体和斜体文本。这很好用。但 AttributedString 输出会忽略换行符 ( )在转换为 AttributedStrings 之前正在工作。有没有办法可以保留粗体斜体文本和换行符? 这是我的代码(相关部分)

struct DetailTabView: View {
var text: String

var body: some View {
    let markdownText: AttributedString = try! AttributedString(markdown: text)

...

Text(markdownText)

举一个 JSON 的例子:

"The word *danjō* simply means platform. *Garan*, which derives from the Sanskrit *saṁghārāma*, refers to the dwelling of a Buddhist monastic community (or *sangha*).\nThe word has been used in Japan to denote to the area of a temple complex where the most essential structures are located."

当我使用 AttributedString 时,单词 danjōGaran 按预期以斜体显示。但是换行符(在 )丢失了。如果我只使用“text”而不转换为 AttributedString,结果会相反。 有人能指出我正确的方向吗?

json swiftui markdown
1个回答
0
投票

init(markdown:
方法提供了
options
,尝试一下
inlineOnlyPreservingWhitespace

let markdownText = try! AttributedString(markdown: text, options: .init(interpretedSyntax: .inlineOnlyPreservingWhitespace)))
© www.soinside.com 2019 - 2024. All rights reserved.