How shall I explain it other than I already did several times?
Showing a code would better explain.
What do I have to do to let the big orange button do what its label says?
All the same. Simply make an Array. You just need more attribute than a single String.
import SwiftUI
struct TextAttribute {
var text: String
var foregroundColor: Color
var borderColor: Color
var borderWidth: CGFloat
}
struct Test: View {
@State var attributes: [TextAttribute] = [
TextAttribute(text: "This is text 1", foregroundColor: .green,
borderColor: .black, borderWidth: 1),
TextAttribute(text: "This is text 2", foregroundColor: .blue,
borderColor: .black, borderWidth: 1),
]
var body: some View {
VStack {
ForEach(0..2) {i in
Text(attributes[i].text)
.foregroundColor(attributes[i].foregroundColor)
.border(attributes[i].borderColor, width: attributes[i].borderWidth)
}
Button(action: {
//Set foregroundColor only of text 1 to purple
attributes[0].foregroundColor = .purple
//make its border width 3
attributes[0].borderWidth = 3
//the border color only of text 2 to gray
attributes[1].borderColor = .gray
}, label: {
Text("Set foregroundColor only of text 1 to purple, make its border width 3 and set the border color only of text 2 to gray")
.font(.system(size: 30))
.foregroundColor(Color.black)
.background(Color.orange)
})
.padding(.top)
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: