FriedPotato
FriedPotato

Reputation: 176

Opt out of SwiftUI Text Markdown Support in iOS 15

let's say I want to show the following text to the user in Text:

3+4*5, 6*7+8

The new SwiftUI behavior in iOS 15 will render this string in Markdown, making the center part italic.

See here: Hacking with Swift

I could do this and it works on iOS 15:

Text("3+4\\*5, 6\\*7+8")

but on iOS 14, it will display as the following due to the lack of Markdown parsing:

3+4\*5, 6\*7+8

Is there a way to opt out of Markdown parsing so I can target iOS 15 and 14 with a single string?

Upvotes: 1

Views: 2051

Answers (2)

Andrew
Andrew

Reputation: 28539

Depending on the initializer that you use, will depend on whether it will render markdown or not. You don't need to use escape characters, you just have to set up your strings in the correct way.

Here is a break down of how you can have or not have markdown with or without localization.

  1. If you use the default Text() and pass a string straight to it then it will assume that you are passing a LocalizedStringKey and it will localize it and render it with mark down. You can read more here.

  2. If you have your string as a variable then SwiftUI will use the Text() initializer where you are using an actual String and it will render it without localization and no markdown. You can read more here.

  3. If you use Text(verbatim:) then it will use no localization and no markdown. You can read more here.

  4. If you want to use localization but no markdown then you need to create a localizedString using String(localized:) and then pass that to the Text(). You can read more here.

struct ContentView: View {

    let stringVariable = "This is *markdown*"

    let localizedString = String(localized: "This is *markdown*")

    var body: some View {
        VStack(spacing: 20) {

            // 1
            // Localization with markdown
            // https://developer.apple.com/documentation/swiftui/text/init(_:tablename:bundle:comment:)
            Text("This is *markdown*")

            // 2
            // No localization no markdown
            // https://developer.apple.com/documentation/swiftui/text/init(_:)-9d1g4
            Text(stringVariable)

            // 3
            // No localization no markdown
            // https://developer.apple.com/documentation/swiftui/text/init(verbatim:)
            Text(verbatim: "This is *markdown*")

            // 4
            // This is a localized string but no markdown
            // https://developer.apple.com/documentation/swift/string/3867985-init
            Text(localizedString)
        }
    }
}

The above produces the following

enter image description here

Upvotes: 9

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

You can create the Text using the verbatim initializer:

Text(verbatim: “1*2 = 2*1”)

This is supported from iOS 13 and will bypass all conversions and localizations.

https://developer.apple.com/documentation/swiftui/text/init(verbatim:)

Upvotes: 2

Related Questions