Skip to content

Instantly share code, notes, and snippets.

@fabiogiolito
Last active December 5, 2018 13:44
Show Gist options
  • Save fabiogiolito/6874f41653ce0ae687c9afd3113eaad2 to your computer and use it in GitHub Desktop.
Save fabiogiolito/6874f41653ce0ae687c9afd3113eaad2 to your computer and use it in GitHub Desktop.
class StyledButton: UIButton {
...
func applyStyles() {
// Apply regular style
self.backgroundColor = .clear
self.titleLabel?.font = Theme.fontButtonText
self.tintColor = Theme.colorShadowLight.cgColor
// Apply different styles
for style in self.styles {
// Sizes
if style == .large {
self.contentEdgeInsets = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
self.titleLabel?.font = Theme.fontButtonTextLarge
self.layer.cornerRadius = 8
}
if style == .wide {
self.contentEdgeInsets.left = self.contentEdgeInsets.top * 2
self.contentEdgeInsets.right = self.contentEdgeInsets.top * 2
}
if style == .tag {
self.contentEdgeInsets = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
self.titleLabel?.font = Theme.fontTag
self.setTitle(self.titleLabel?.text?.uppercased(), for: .normal)
self.backgroundColor = Theme.colorBackgroundMuted
self.layer.borderWidth = 0
}
// Background
if style == .primary {
self.backgroundColor = Theme.colorPrimary
self.tintColor = Theme.colorPrimaryContrast
self.layer.borderWidth = 0
}
if style == .secondary {
self.backgroundColor = Theme.colorSecondary
self.tintColor = Theme.colorSecondaryContrast
self.layer.borderWidth = 0
}
// Tint
if style == .tintPrimary {
self.tintColor = Theme.colorPrimary
}
if style == .tintSecondary {
self.tintColor = Theme.colorSecondary
}
...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment