Advertisement
Guest User

Untitled

a guest
May 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.70 KB | None | 0 0
  1. struct SubscriptionButtonViewModel {
  2.     let pediodText: String
  3.     let priceText: String
  4.     let backgroundColor: UIColor
  5.     let textColor: UIColor
  6.     let borderColor: UIColor?
  7. }
  8.  
  9. class SubscriptionButtonView: UIButton {
  10.    
  11.     private enum Constants {
  12.         static let basicFont: UIFont = UIFont.fontRobotoBoldOf(size: 20)
  13.         static let cornerRadius: CGFloat = 4.0
  14.         static let defaultBordedWidth: CGFloat = 1.5
  15.     }
  16.    
  17.     enum ViewConstants {
  18.         static let defaultButtonHeight: CGFloat = 60.0
  19.     }
  20.    
  21.     private weak var pediodLabel: UILabel!
  22.     private weak var priceLabel: UILabel!
  23.    
  24.     class func create(with model: SubscriptionButtonViewModel) -> SubscriptionButtonView {
  25.        
  26.         let view = SubscriptionButtonView()
  27.         view.configure(with: model)
  28.        
  29.         return view
  30.     }
  31. }
  32.  
  33. private extension SubscriptionButtonView {
  34.    
  35.     func createPeriodLabel(with model: SubscriptionButtonViewModel) -> UILabel {
  36.         let label = UILabel()
  37.         label.textColor = model.textColor
  38.         label.font = Constants.basicFont
  39.         label.text = model.pediodText
  40.         label.textAlignment = .left
  41.        
  42.         label.translatesAutoresizingMaskIntoConstraints = false
  43.         addSubview(label)
  44.         label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
  45.         label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
  46.  
  47.         return label
  48.     }
  49.    
  50.     func createPriceLabel(with model: SubscriptionButtonViewModel) -> UILabel {
  51.         let label = UILabel()
  52.         label.textColor = model.textColor
  53.         label.font = Constants.basicFont
  54.         label.text = model.priceText
  55.         label.textAlignment = .right
  56.        
  57.         label.translatesAutoresizingMaskIntoConstraints = false
  58.         addSubview(label)
  59.         label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
  60.         label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
  61.        
  62.         return label
  63.     }
  64.    
  65.     func createViews(with model: SubscriptionButtonViewModel) {
  66.         pediodLabel = createPeriodLabel(with: model)
  67.         priceLabel = createPriceLabel(with: model)
  68.         backgroundColor = model.backgroundColor
  69.         layer.cornerRadius = Constants.cornerRadius
  70.         clipsToBounds = true
  71.         if let borderColor = model.borderColor {
  72.             layer.borderColor = borderColor.cgColor
  73.             layer.borderWidth = Constants.defaultBordedWidth
  74.         }
  75.         translatesAutoresizingMaskIntoConstraints = false
  76.     }
  77.    
  78.     func configure(with model: SubscriptionButtonViewModel) {
  79.         createViews(with: model)
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement