Skip to main content

How to render Markdown content in text

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to render Markdown content in text 관련

SwiftUI by Example

Back to Home

How to render Markdown content in text | SwiftUI by Example

How to render Markdown content in text

Updated for Xcode 15

New in iOS 15

SwiftUI has built-in support for rendering Markdown, including bold, italic, links, and more. It's literally built right into SwiftUI's Text view, so you can write code like this:

VStack {
    Text("This is regular text.")
    Text("* This is **bold** text, this is *italic* text, and this is ***bold, italic*** text.")
    Text("~~A strikethrough example~~")
    Text("`Monospaced works too`")
    Text("Visit Apple: [click here](https://apple.com)")
}

Download this as an Xcode projectopen in new window

Several lines of text appropriately formatted with Markdown styling.

And yes, that link is automatically tappable. Markdown links will use your app's accent color by default, but you can change that using the tint() modifier:

Text("Visit Apple: [click here](https://apple.com)")
    .tint(.indigo)

Download this as an Xcode projectopen in new window

Note

Images aren't supported.

This automatic Markdown conversion happens because SwiftUI interprets those strings as being instances of LocalizedStringKey – strings that can be localized by our app. This means if you want to create Markdown text from a property or variable, you should mark it explicitly as being LocalizedStringKey to get the Markdown rendering:

struct ContentView: View {
    let markdownText: LocalizedStringKey = "* This is **bold** text, this is *italic* text, and this is ***bold, italic*** text."

    var body: some View {
        Text(markdownText)
    }
}

Download this as an Xcode projectopen in new window

If you wanted the original text unchanged – i.e., you wanted the raw, unformatted Markdown symbols to be left in place – just remove the LocalizedStringKey annotation. Alternatively, you can disable both Markdown and localization entirely using the Text(verbatim:) initializer.

Similar solutions…
How to render a SwiftUI view to a PDF | SwiftUI by Example

How to render a SwiftUI view to a PDF
How to render a gradient | SwiftUI by Example

How to render a gradient
How to render images using SF Symbols | SwiftUI by Example

How to render images using SF Symbols
How to format text inside text views | SwiftUI by Example

How to format text inside text views
How to create scrolling pages of content using tabViewStyle() | SwiftUI by Example

How to create scrolling pages of content using tabViewStyle()

이찬희 (MarkiiimarK)
Never Stop Learning.