Mobias
Mobias

Reputation: 31

How to format Text bold from a String in SwiftUI?

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

Upvotes: 1

Views: 509

Answers (1)

Benzy Neez
Benzy Neez

Reputation: 21730

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

Upvotes: 2

Related Questions