Enhancing Your SwiftUI App with Markdown

SwiftUI has brought a lot of excitement to the iOS development community with its declarative syntax and powerful features. One of the lesser-known but highly useful features in SwiftUI is the ability to use Markdown for text formatting. Markdown allows you to add rich text formatting to your SwiftUI views with ease. In this blog post, we’ll explore how to use Markdown in SwiftUI, pass it between views using arguments, and highlight the supported Markdown features. This will not only make your app more visually appealing but also enhance the user experience.

Introduction to Markdown in SwiftUI

Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents. In SwiftUI, you can use Markdown to create bold text, italic text, lists, and more. This can be particularly useful when you want to display rich text content without having to deal with complex HTML or other formatting languages.

Here's a simple example of using Markdown in SwiftUI:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("**Hello**, _SwiftUI_!")
            .padding()
    }
}

In the code above, the Text view is used to display the text with Markdown formatting. The **Hello** part makes the word "Hello" bold, and the _SwiftUI_ part makes the word "SwiftUI" italic.

Supported Markdown Features in SwiftUI

SwiftUI supports a range of Markdown features that you can use to enhance your text. Here are some of the key features:

  • Bold Text: Use double asterisks **bold** or double underscores __bold__ to make text bold.
  • Italic Text: Use single asterisk *italic* or single underscore _italic_ to make text italic.
  • Bold and Italic Text: Combine asterisks ***bold and italic*** to make text bold and italic.
  • Strikethrough: Use double tildes ~~strikethrough~~ to strike through text.
  • Inline Code: Use backticks `code` to format inline code.
  • Links: Use [link text](URL) to create hyperlinks.
  • Headings: Use hashtags # for headings. One # for H1, two ## for H2, etc.
  • Lists: Use - or * for unordered lists, and numbers for ordered lists.

Here's an example demonstrating these features:

import SwiftUI

struct MarkdownExampleView: View {
    let markdownText: String = """
    # Heading 1
    ## Heading 2
    ### Heading 3
    
    **Bold Text**
    _Italic Text_
    ***Bold and Italic Text***
    
    ~~Strikethrough Text~~
    
    `Inline Code`
    
    [SwiftUI Documentation](https://developer.apple.com/documentation/swiftui)
    
    - Unordered List Item 1
    - Unordered List Item 2
    
    1. Ordered List Item 1
    2. Ordered List Item 2
    """
    
    var body: some View {
        ScrollView {
            Text(markdownText)
                .padding()
        }
    }
}

Passing Markdown Text Between Views

One of the strengths of SwiftUI is its ability to pass data between views easily. You might want to pass Markdown text to different views to keep your code modular and maintainable. To achieve this, you can use view arguments. Let’s see how this can be done.

First, define a view that takes a Markdown string as an argument:

import SwiftUI

struct MarkdownTextView: View {
    let markdownText: String
    
    var body: some View {
        Text(markdownText)
            .padding()
    }
}

Now, you can use this custom view in your main view and pass Markdown text to it:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            MarkdownTextView(markdownText: "**Hello**, _SwiftUI_!")
            MarkdownTextView(markdownText: "Welcome to **Swift** programming.")
        }
    }
}

In the ContentView, we use MarkdownTextView twice, passing different Markdown strings to it. This approach keeps our code clean and modular.

Dynamic Markdown Text

You might also want to pass dynamic Markdown text to your views. This can be done by using state variables. Let’s modify our example to demonstrate this:

import SwiftUI

struct ContentView: View {
    @State private var markdownText: String = "**Hello**, _SwiftUI_!"
    
    var body: some View {
        VStack {
            MarkdownTextView(markdownText: markdownText)
            Button(action: {
                markdownText = "Updated **Markdown** text."
            }) {
                Text("Update Text")
            }
            .padding()
        }
    }
}

In this example, we use a @State variable to hold the Markdown text. When the button is pressed, the Markdown text is updated, and the view automatically reflects the change. This demonstrates the power of SwiftUI's declarative syntax and state management.

Conclusion

Using Markdown in SwiftUI is a great way to add rich text formatting to your app without the complexity of HTML or other markup languages. By passing Markdown text between views, you can keep your code modular and maintainable. Whether you're displaying static or dynamic content, SwiftUI makes it easy to integrate Markdown into your app. Give it a try in your next project and see how it can enhance your user interface!

Our mission

Effect UI for
your next project

We are a team of talented designers making iOS components to help developers build outstanding apps faster with less effort and best design.