1

i have this example code:

let longString = """

imagine here is a really really long Text. **This is in bold**. This is not in bold
"""

struct ContentView: View {
    var body: some View {
       
            Text(longString)
     
    }
}

But this is not working. I get the Text with the ** in the View, but I want it in bold.

If I do this:

Text("imagine here is a really really long Text. **This is in bold**. This is not in bold")

This works.

Why is that and how I can use bold Text from a String in this case?

Thank you for taking your time to help me

Mobias
  • 31
  • 3
  • https://www.hackingwithswift.com/quick-start/swiftui/how-to-render-markdown-content-in-text – jnpdx Mar 24 '23 at 18:12

1 Answers1

1

The version with the string literal is going to Text.init(LocalizedStringKey, tableName: String?, bundle: Bundle?, comment: StaticString?), in other words, the string literal is being handled as a LocalizedStringKey.

If you want to use a property instead of a string literal then you need to convert it to a LocalizedStringKey explicitly:

Text(LocalizedStringKey(longString))
Benzy Neez
  • 1,546
  • 2
  • 3
  • 10