Last active
February 21, 2024 09:16
-
-
Save benigumocom/b1a2563dcbf54944560035a79b15dc1f to your computer and use it in GitHub Desktop.
【SwiftUI】カスタム ViewModifier の使い方と使いどころ 🤔 👉 https://android.benigumo.com/20240221/custom-viewmodifier/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Author
benigumocom
commented
Feb 21, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment