> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swiftui/control/text/markdown.md).

# Markdown

{% hint style="info" %}
In [WWDC 2021](https://developer.apple.com/videos/play/wwdc2021/10018/) Apple announced to add [Markdown](https://en.wikipedia.org/wiki/Markdown) support to the <mark style="color:red;">**Foundation**</mark> (<mark style="color:purple;">**`AttributedString`**</mark>) and <mark style="color:red;">**SwiftUI**</mark> (<mark style="color:purple;">**`Text`**</mark>) starting with <mark style="color:orange;">**iOS 15**</mark>.
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}
![](/files/HyPdGQa1haExFmqidxED)

```swift
// ⭐️ Markdown works with string literals
Text("~~strikethrough~~")
Text("**bold**\n*italic*")    // line breaks OK

// ⭐️ Markdown doesn't work with string variables/interpolations
let markdown = "**Doesn't** *Work* with string variables/interpolations."
Text(markdown).foregroundColor(.pink)
Text(verbatim: markdown)
Text("\(markdown)")
Text(verbatim: "\(markdown)")

// ------------------------
//     AttributedString
// ------------------------

// ⭐️ AttributedString supports string variables
let str = "`AttributedString` **DOES** support *string variables*"
let attrStrSupportStrVar = try! AttributedString(markdown: str)

// ⭐️ AttributedString ignores line breaks by default.
let lineBreaksIgnored = try! AttributedString(
    markdown: "**No**\n~~*Line*~~\n`Breaks`!"
)
// ⭐️ add option to support line breaks.
let respectLineBreaks = try! AttributedString(
    markdown: "**Line 1**\n~~*Line 2*~~\n!", 
    options: .init(interpretedSyntax: .inlineOnlyPreservingWhitespace)
)

Text(attrStrSupportStrVar).foregroundColor(.green)
Text(lineBreaksIgnored).foregroundColor(.pink)
Text(respectLineBreaks)
    .multilineTextAlignment(.center)
    .foregroundColor(.blue)
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] Marco ⟩ [3 surprises when using Markdown in SwiftUI](https://blog.eidinger.info/3-surprises-when-using-markdown-in-swiftui?utm_campaign=iOS%2BDev%2BWeekly\&utm_medium=email\&utm_source=iOS%2BDev%2BWeekly%2BIssue%2B545)
  {% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}
SwiftUI supports [GitHub Flavored Markdown](https://github.github.com/gfm/) (GFM).
{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}

* supported by [AttributedString](/ios/swift/type/category/basic/string/attributedstring.md).
* supported by [Text](/ios/swiftui/control/text.md).
  {% endtab %}
  {% endtabs %}
