从一个AttributedString中获取原始的Markdown字符串在Swift中

huangapple go评论69阅读模式
英文:

Getting the raw Markdown string from an AttributedString in Swift

问题

在一个 AttributedString 中获取 Markdown 字符串的方法是什么?我有以下代码:

let text = "**Hello** World!"
let attString = AttributedString(text)

现在我想要获取Markdown字符串。我看到 AttributedString 包括一个 description 调用来尝试解决这个问题,但它会在结果中添加一组括号:

print(attString.description)

结果:

**Hello** World! {
}

我认为可能有更好的方法来做到这一点。

英文:

Is there a way to get the Markdown string from an AttributedString? I have the following code:

let text = "**Hello** World!"
let attString = AttributedString(text)

Now I want to get back the Markdown string.
I did see that AttributedString includes a description call that attempts to address the problem, but it adds a set of brackets to the result:

print(attString.description)

Result:

**Hello** World! {
}

I thought there might be a better way of doing this.

答案1

得分: 1

Markdown 是一种用于方便构建 AttributedString 的方式。它并不是 AttributedString 的内部格式。大多数 AttributedString 可以表示的内容在 Markdown 中甚至无法表示。在最基本的示例中,AttributedString 可以定义区域的确切字体。在 Markdown 中无法表达这一点。在极端情况下,AttributedString 可以表示任何属性,包括您的代码中定义的自定义属性,而不仅仅是 Foundation 定义的属性。

但在您的情况下,我认为混淆更基本一些。所谓的 "Markdown" 只是字符串。如果您需要它,可以使用以下方式获取:

String(attString.characters)

(令人惊讶的是,没有 .string 属性。这是因为它的构造比看起来要昂贵。

您在这里构建的不是由 Markdown 定义的 AttributedString。这需要调用 try AttributedString(markdown: text)。它只是一个具有星号的字符串。如果您确实使用 Markdown 创建了格式化的 AttributedString,并且需要获取 Markdown,则需要在包装类型中自行跟踪它,该包装类型包含一个 AttributedString 和 Markdown 字符串。

英文:

Markdown is a way to construct AttributedString as a convenience. It is not the internal format of AttributedString. Most things that an AttributedString can represent cannot even be represented in Markdown. In the most basic example, AttributedString can define the exact font for a region. There is no way to express that in Markdown. At the extreme, AttributedString can represent any attributes, including custom ones defined in your code, not just ones defined by Foundation.

But in your case, I believe the confusion is more basic. The "Markdown" in question is just the string. If you want that, then that's fetched with:

String(attString.characters)

(Somewhat surprisingly there is no .string property. This is because it's more expensive to construct than it looks.)

What you've built here isn't an AttributedString defined by Markdown. That would require calling try AttributedString(markdown: text). It's just a string that happens to have asterisks in it. If you really did create a formatted AttributedString with Markdown, and needed to get the Markdown back, you would need to track that yourself in a wrapper type that contained an AttributedString and the Markdown String.

答案2

得分: -1

以下是翻译好的代码部分:

let text = "**你好** 世界!"
let attString = AttributedString(text)
let data = try JSONEncoder().encode(attString)
if let stringValue = String(data: data, encoding: .utf8) {
    print(stringValue)
}
英文:

As suggested by @paulo-mattos here is the solution:

let text = "**Hello** World!"
let attString = AttributedString(text)
let data = try JSONEncoder().encode(attString)
if let stringValue = String( data: data, encoding:.utf8 ){
    print( stringValue)
}

huangapple
  • 本文由 发表于 2023年5月15日 03:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定