🤖
SwiftUIのModifierはまとめてデザインを変更する時に便利。
最近はModifierを使ってなかった。
まとめてスタイルを変更できるので便利かも。
import SwiftUI
enum Styles {
case normal, accent
var modifier: LargeText {
switch self {
case .normal:
return LargeText(color: .red)
case .accent:
return LargeText(color: .blue)
}
}
}
struct ContentView: View {
@State private var style = Styles.normal
var body: some View {
VStack {
title("Hello, World!")
title("Hello, World!")
title("Hello, World!")
}
.modifier(style.modifier)
.onTapGesture {
style = .accent
}
}
private func title(_ title: String) -> some View {
Text(title)
}
}
struct LargeText: ViewModifier {
let color: Color
func body(content: Content) -> some View {
content
.font(.largeTitle)
.foregroundColor(color)
.border(Color.red, width: 1)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
}
}
Discussion