I have a simple code that I used three times Text with identical parameters but with different Text. How to avoid code duplication? I used SwiftUI and Xcode 13.4
Text(status)
.fontWeight(.bold)
.foregroundColor(Color.white)
.padding(.leading, 5)
.padding(.trailing, 5)
.background(stats)
.cornerRadius(3)
2
Answers
You can create a custom
ViewModifier
for yourText
.You can create an extension of
View
like this to use the aboveViewModifier
.Now set this
ViewModifier
to yourText
.You can simply DRY it by converting it to a function:
Then use it like: