Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. //
  2. // StaticCell.swift
  3. // Yarche
  4. //
  5. // Created by Olga Kovrigina on 31/01/2018.
  6. // Copyright © 2018 MAG Development. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import PinLayout
  11. import UIKit
  12.  
  13. @IBDesignable
  14. class RowButton: UIControl {
  15.  
  16. let selectionView = UIView()
  17.  
  18. let titleLabel = UILabel().withStyle(.cellTitle)
  19.  
  20. let rightImageView = UIImageView()
  21.  
  22. let leftImageView = UIImageView()
  23.  
  24. let hintLabel = UILabel() ~> {
  25. $0.font = AppTheme.Font.H6
  26. $0.textColor = AppTheme.Color.tangerine
  27. $0.textAlignment = .right
  28. }
  29.  
  30. let topSeparator = UIView() ~> {
  31. $0.backgroundColor = AppTheme.Color.silver
  32. }
  33.  
  34. let bottomSeparator = UIView() ~> {
  35. $0.backgroundColor = AppTheme.Color.silver
  36. }
  37.  
  38. @IBInspectable var title: String = "" {
  39. didSet {
  40. titleLabel.text = title
  41. setNeedsLayout()
  42. }
  43. }
  44.  
  45. @IBInspectable var hint: String? {
  46. didSet {
  47. hintLabel.text = hint
  48. setNeedsLayout()
  49. }
  50. }
  51.  
  52. @IBInspectable var hintColor: UIColor = AppTheme.Color.tangerine {
  53. didSet { hintLabel.textColor = hintColor }
  54. }
  55.  
  56. @IBInspectable var rightIcon: UIImage? = nil {
  57. didSet { rightImageView.image = rightIcon }
  58. }
  59.  
  60. @IBInspectable var leftIcon: UIImage? = nil {
  61. didSet { leftImageView.image = leftIcon }
  62. }
  63.  
  64. @IBInspectable var isTopSeparatorVisible: Bool = true
  65. @IBInspectable var isBottomSeparatorVisible: Bool = true
  66.  
  67. @IBInspectable var topSeparatorMargin: CGFloat = 0
  68. @IBInspectable var bottomSeparatorMargin: CGFloat = 0
  69. private let separatorHeight: CGFloat = 1 / UIScreen.main.scale
  70.  
  71. private let highlightColor = UIColor(hex: 0xD9D9D9)
  72.  
  73. override var isHighlighted: Bool {
  74. didSet {
  75. selectionView.backgroundColor = isHighlighted
  76. ? highlightColor
  77. : UIColor.clear
  78. }
  79. }
  80.  
  81. override init(frame: CGRect) {
  82. super.init(frame: frame)
  83. commonInit()
  84. }
  85.  
  86. required init?(coder aDecoder: NSCoder) {
  87. super.init(coder: aDecoder)
  88. commonInit()
  89. }
  90.  
  91. private func commonInit() {
  92. addSubviews(selectionView, leftImageView, titleLabel, hintLabel, rightImageView, topSeparator, bottomSeparator)
  93. }
  94.  
  95. override func layoutSubviews() {
  96. super.layoutSubviews()
  97. layout()
  98. }
  99.  
  100. private func layout() {
  101. selectionView.frame = self.bounds
  102.  
  103. if isTopSeparatorVisible {
  104. topSeparator.pin.top().left(topSeparatorMargin).right().height(separatorHeight)
  105. }
  106. if isBottomSeparatorVisible {
  107. bottomSeparator.pin.bottom().left(bottomSeparatorMargin).right().height(separatorHeight)
  108. }
  109.  
  110. let leftIconSide: CGFloat = leftIcon == nil ? 0 : 24
  111. leftImageView.pin.left(16).vCenter().width(leftIconSide).height(leftIconSide)
  112.  
  113. let rightIconSide: CGFloat = rightIcon == nil ? 0 : 24
  114. rightImageView.pin.right(16).vCenter().width(rightIconSide).height(rightIconSide)
  115.  
  116. hintLabel.pin
  117. .before(of: rightImageView)
  118. .marginLeft(8)
  119. .marginRight(rightIcon == nil ? 0 : 8)
  120. .vCenter()
  121. .sizeToFit(.width)
  122.  
  123. titleLabel.pin
  124. .horizontallyBetween(leftImageView, and: hintLabel)
  125. .top(12)
  126. .marginLeft(leftIcon == nil ? 0 : 16)
  127. .sizeToFit(.width)
  128. }
  129.  
  130. override func sizeThatFits(_ size: CGSize) -> CGSize {
  131. layout()
  132. return CGSize(width: bounds.size.width, height: titleLabel.frame.maxY + 12)
  133. }
  134.  
  135. override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
  136. guard self.point(inside: point, with: event) else {
  137. return nil
  138. }
  139.  
  140. return self
  141. }
  142.  
  143. override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
  144. isHighlighted = true
  145. return true
  146. }
  147.  
  148. override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
  149. guard point(inside: touch.location(in: self), with: event) else {
  150. isHighlighted = false
  151. return false
  152. }
  153.  
  154. return true
  155. }
  156.  
  157. override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
  158. isHighlighted = false
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement