I am trying our SwiftUI and want to create a component on the lines of SwiftUI's components. So, here is what I am trying to do:
Create a new view extending View
struct CustomComponent: View {
var title: String
var body: some View {
HStack {
Image(systemName: "") // This would be updated through style
Text(verbatim: title)
}
}
}
extension View {
public func componentStyle<S>(_ style: S) -> some View where S : ComponentStyle {
guard self is CustomComponent else {
return AnyView(self)
}
return AnyView(
// Here I want to add the spacing attribute of the style to the HStack.
// Also I want to update the Image with the corresponding style's icon.
// If it's not possible here, please suggest alternate place.
self
.foregroundColor(style.tintColor)
.frame(height: style.height)
)
}
}
public protocol ComponentStyle {
var icon: String { get }
var tintColor: Color { get }
var spacing: CGFloat { get }
var height: CGFloat { get }
}
struct ErrorStyle: ComponentStyle {
var icon: String {
return "xmark.octagon"
}
var tintColor: Color {
return .red
}
var spacing: CGFloat {
return 8
}
var height: CGFloat {
return 24
}
}
How can I achieve the following:
- How can I add the spacing attribute of the style to the HStack?
- How can I update the Image with the corresponding style's icon?