How to format Text bold from a String in SwiftUI?

319 Views Asked by At

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

1

There are 1 best solutions below

0
Benzy Neez On

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))