oOEric
oOEric

Reputation: 1079

Can't show 3 kinds of color in a Text in SwiftUI

I want to make a label using SwiftUI showing text in 3 colors i.e. ABC (A in black, B in gray, C in red) by following code

let aString = "A"
let bString = "B"
let cString = "C"

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("\(aString)\(Text(bString).foregroundColor(Color.gray))\(Text(cString).foregroundColor(Color.red))")
            .foregroundColor(Color.black)
            .padding()
    }
}

But the result is enter image description here

The last text 1 is unexpected.

The iOS project is for iOS 14
I run the app at iPad Pro 11-inch 2nd gen, iPadOS 14.1

Upvotes: 0

Views: 98

Answers (1)

cedricbahirwe
cedricbahirwe

Reputation: 1396

A clean and clear approach that still produce the expected result would be:

Text(aString).foregroundColor(.black)
+  Text(bString).foregroundColor(.gray)
+  Text(cString).foregroundColor(.red)

Upvotes: 2

Related Questions