Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active February 21, 2024 09:16
Show Gist options
  • Save benigumocom/b1a2563dcbf54944560035a79b15dc1f to your computer and use it in GitHub Desktop.
Save benigumocom/b1a2563dcbf54944560035a79b15dc1f to your computer and use it in GitHub Desktop.
【SwiftUI】カスタム ViewModifier の使い方と使いどころ 🤔 👉 https://android.benigumo.com/20240221/custom-viewmodifier/
import SwiftUI
struct TestView: View {
var body: some View {
VStack {
Text("top")
Text("bottom")
}
VStack {
Text("top")
Text("bottom")
}
.modifier(TitleTextFormat())
VStack {
Text("top")
Text("bottom")
}
.titleTextFormat()
VStack {
Text("top")
Text("bottom")
}
.titleTextFormat2()
TitleTextFormatView {
VStack {
Text("top")
Text("bottom")
}
}
TitleTextFormatView {
Text("top")
Text("bottom")
}
}
}
struct TitleTextFormat: ViewModifier {
func body(content: Content) -> some View {
content
.font(.title)
.foregroundColor(.red)
}
}
extension View {
func titleTextFormat() -> some View {
modifier(TitleTextFormat())
}
}
extension View {
func titleTextFormat2() -> some View {
self
.font(.title)
.foregroundColor(.red)
}
}
struct TitleTextFormatView<Content: View>: View {
@ViewBuilder var content: Content
var body: some View {
content
.font(.title)
.foregroundColor(.red)
}
}
#Preview {
TestView()
}
@benigumocom
Copy link
Author

sc 2024-02-21 at 18 15 38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment