Skip to content

Instantly share code, notes, and snippets.

@vince19972
Last active September 9, 2019 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vince19972/ffb1d1757fd847120611cc73f190d1be to your computer and use it in GitHub Desktop.
Save vince19972/ffb1d1757fd847120611cc73f190d1be to your computer and use it in GitHub Desktop.
Demo code of buttonStyle in SwiftUI
struct TokenButtonStyle: ButtonStyle {
/// Global properties
private let styleType: StyleType
private var iconSize: TokenButton.IconSize?
private var backgroundColor: Color?
private var borderStyle: StyleAlias.BorderStyle?
private var textColor: Color?
/// Circle Icon Button
init(iconSize: TokenButton.IconSize,
backgroundColor: BackgroundColor,
border: BorderStyle? = .none) {
// --snip--
}
/// Icon Button
init(iconSize: TokenButton.IconSize) {
// --snip--
}
/// Capsule Button
init(backgroundColor: BackgroundColor, textColor: TextColor) {
// --snip--
}
/// Text Button
init(textColor: TextColor) {
// --snip--
}
/// Rendering Button Style
func makeBody(configuration: Self.Configuration) -> some View {
var renderView: AnyView!
let verticalMargin = CapsuleValue.verticalMargin.rawValue
let horizontalMargin = CapsuleValue.horizontalMargin.rawValue
switch styleType {
case .circleIcon:
renderView = AnyView(configuration.label
// --snip--
)
case .icon:
renderView = AnyView(configuration.label
// --snip--
)
case .capsule:
renderView = AnyView(configuration.label
// --snip--
)
case .text:
renderView = AnyView(configuration.label
// --snip--
)
}
return renderView.opacity(getOpacityValue(configuration.isPressed))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment