SwiftUI: Markdown support for string variables

Text in iOS 15 Beta 1 (Xcode 13 Beta 1) only handles markdown when string literal was passed to initializer.

struct MarkdownTest: View {
  var text: String = "**Hello** *World*"
  var body: some View {
    VStack {
      Text("**Hello** *World*") // will be rendered with markdown formatting
      Text(text) // will NOT be rendered according to markdown
    }
  }
}

struct MarkdownTestPreviews: PreviewProvider {
  static var previews: some View {
    MarkdownTest()
  }
}

Is this a known bug or do I have to create an entry in Feedback Assistant?

Is this a known bug or do I have to create an entry in Feedback Assistant?

I'm not sure this would be considered to be a bug or not. When you pass a String literal for Text.init, Swift uses different initializer than passing a value of String.

Structure Text

Localized Strings

If you initialize a text view with a string literal, the view uses the init(_:tableName:bundle:comment:) initializer,

Please try this:

struct MarkdownTest: View {
    var text: String = "**Hello** *World*"
    var markdownText: AttributedString = try! AttributedString(markdown: "**Hello** *World*")
    var keyText: LocalizedStringKey = "**Hello** *World*"
    
    var body: some View {
        VStack {
            Text("**Hello** *World*")
            Text(text)
            
            Text("**Hello** *World*" as String)
            Text(markdownText)
            Text(keyText)
        }
    }
}

I have the problem that creating markdown with AttributedString(markdown: "Hello World") works, but it ignores linefeed "\n" characters and creates one big mess in one line. How to create paragraphs ?

Try this:

struct MarkdownTest: View {
  var text: String = "**Hello** *World*"
  var body: some View {
    VStack {
      Text("**Hello** *World*") // will be rendered with markdown formatting
      Text(.init(text)) // this renders markdown properly
    }
  }
}

struct MarkdownTestPreviews: PreviewProvider {
  static var previews: some View {
    MarkdownTest()
  }
}
SwiftUI: Markdown support for string variables
 
 
Q