SHARE
TWEET

OvalButtonWithShadow

a guest Sep 17th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  CoveOvalButton.swift
  3. //  cove-ios
  4. //
  5. //  Created by Josip Rezic on 14/08/2019.
  6. //
  7.  
  8. import cove_ios_styling
  9. import SnapKit
  10.  
  11. final class CoveOvalButton: UIButton {
  12.    
  13.     static let defaultHeight = 60.0
  14.     static let defaultWidth = 207.0
  15.    
  16.     private let style: CoveOvalButtonStyle
  17.    
  18.     override var bounds: CGRect {
  19.         didSet {
  20.             roundCorners()
  21.         }
  22.     }
  23.    
  24.     override var isEnabled: Bool {
  25.         didSet {
  26.             updateStyling()
  27.         }
  28.     }
  29.    
  30.     override var isHighlighted: Bool {
  31.         didSet {
  32.             updateStyling()
  33.         }
  34.     }
  35.    
  36.     init(style: CoveOvalButtonStyle) {
  37.         self.style = style
  38.         super.init(frame: .zero)
  39.         updateStyling()
  40.     }
  41.    
  42.     required init?(coder aDecoder: NSCoder) {
  43.         fatalError("init(coder:) has not been implemented")
  44.     }
  45.    
  46.     override func setTitle(_ title: String?, for state: UIControl.State) {
  47.         super.setTitle(title?.uppercased(), for: state)
  48.     }
  49.    
  50.     private func updateStyling() {
  51.         setupUI(textColor: style.titleColor, font: style.font)
  52.         setTitleColor(style.titleColorDisabled, for: .disabled)
  53.        
  54.         roundCorners()
  55.         setBackgroundColor()
  56.         addBorder()
  57.         addShadow()
  58.     }
  59.    
  60.     private func roundCorners() {
  61.         layer.cornerRadius = frame.height / 2.0
  62.     }
  63.    
  64.     private func setBackgroundColor() {
  65.         if isEnabled {
  66.             backgroundColor = isHighlighted ? style.backgroundColorHighlighted : style.backgroundColor
  67.         } else {
  68.             backgroundColor = style.backgroundColorDisabled
  69.         }
  70.     }
  71.    
  72.     private func addShadow() {
  73.         layer.shadowColor = Style.Color.defaultShadow.cgColor
  74.         layer.shadowOffset = CGSize(width: 0, height: 10)
  75.         layer.shadowOpacity = 1.0
  76.         layer.shadowRadius = 10.0
  77.         layer.masksToBounds = false
  78.     }
  79.    
  80.     private func addBorder() {
  81.         if isEnabled {
  82.             addBorder(color: style.borderColor, width: style.borderWidth)
  83.         } else {
  84.             addBorder(color: style.borderColorDisabled, width: style.borderWidthDisabled)
  85.         }
  86.     }
  87.    
  88.     private func addBorder(color: UIColor, width: CGFloat = 1.0) {
  89.         layer.borderColor = color.cgColor
  90.         layer.borderWidth = width
  91.     }
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top