Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CoveOvalButton.swift
- // cove-ios
- //
- // Created by Josip Rezic on 14/08/2019.
- //
- import cove_ios_styling
- import SnapKit
- final class CoveOvalButton: UIButton {
- static let defaultHeight = 60.0
- static let defaultWidth = 207.0
- private let style: CoveOvalButtonStyle
- override var bounds: CGRect {
- didSet {
- roundCorners()
- }
- }
- override var isEnabled: Bool {
- didSet {
- updateStyling()
- }
- }
- override var isHighlighted: Bool {
- didSet {
- updateStyling()
- }
- }
- init(style: CoveOvalButtonStyle) {
- self.style = style
- super.init(frame: .zero)
- updateStyling()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func setTitle(_ title: String?, for state: UIControl.State) {
- super.setTitle(title?.uppercased(), for: state)
- }
- private func updateStyling() {
- setupUI(textColor: style.titleColor, font: style.font)
- setTitleColor(style.titleColorDisabled, for: .disabled)
- roundCorners()
- setBackgroundColor()
- addBorder()
- addShadow()
- }
- private func roundCorners() {
- layer.cornerRadius = frame.height / 2.0
- }
- private func setBackgroundColor() {
- if isEnabled {
- backgroundColor = isHighlighted ? style.backgroundColorHighlighted : style.backgroundColor
- } else {
- backgroundColor = style.backgroundColorDisabled
- }
- }
- private func addShadow() {
- layer.shadowColor = Style.Color.defaultShadow.cgColor
- layer.shadowOffset = CGSize(width: 0, height: 10)
- layer.shadowOpacity = 1.0
- layer.shadowRadius = 10.0
- layer.masksToBounds = false
- }
- private func addBorder() {
- if isEnabled {
- addBorder(color: style.borderColor, width: style.borderWidth)
- } else {
- addBorder(color: style.borderColorDisabled, width: style.borderWidthDisabled)
- }
- }
- private func addBorder(color: UIColor, width: CGFloat = 1.0) {
- layer.borderColor = color.cgColor
- layer.borderWidth = width
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement