Reputation: 1079
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()
}
}
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
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