Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // StaticCell.swift
- // Yarche
- //
- // Created by Olga Kovrigina on 31/01/2018.
- // Copyright © 2018 MAG Development. All rights reserved.
- //
- import Foundation
- import PinLayout
- import UIKit
- @IBDesignable
- class RowButton: UIControl {
- let selectionView = UIView()
- let titleLabel = UILabel().withStyle(.cellTitle)
- let rightImageView = UIImageView()
- let leftImageView = UIImageView()
- let hintLabel = UILabel() ~> {
- $0.font = AppTheme.Font.H6
- $0.textColor = AppTheme.Color.tangerine
- $0.textAlignment = .right
- }
- let topSeparator = UIView() ~> {
- $0.backgroundColor = AppTheme.Color.silver
- }
- let bottomSeparator = UIView() ~> {
- $0.backgroundColor = AppTheme.Color.silver
- }
- @IBInspectable var title: String = "" {
- didSet {
- titleLabel.text = title
- setNeedsLayout()
- }
- }
- @IBInspectable var hint: String? {
- didSet {
- hintLabel.text = hint
- setNeedsLayout()
- }
- }
- @IBInspectable var hintColor: UIColor = AppTheme.Color.tangerine {
- didSet { hintLabel.textColor = hintColor }
- }
- @IBInspectable var rightIcon: UIImage? = nil {
- didSet { rightImageView.image = rightIcon }
- }
- @IBInspectable var leftIcon: UIImage? = nil {
- didSet { leftImageView.image = leftIcon }
- }
- @IBInspectable var isTopSeparatorVisible: Bool = true
- @IBInspectable var isBottomSeparatorVisible: Bool = true
- @IBInspectable var topSeparatorMargin: CGFloat = 0
- @IBInspectable var bottomSeparatorMargin: CGFloat = 0
- private let separatorHeight: CGFloat = 1 / UIScreen.main.scale
- private let highlightColor = UIColor(hex: 0xD9D9D9)
- override var isHighlighted: Bool {
- didSet {
- selectionView.backgroundColor = isHighlighted
- ? highlightColor
- : UIColor.clear
- }
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
- commonInit()
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- commonInit()
- }
- private func commonInit() {
- addSubviews(selectionView, leftImageView, titleLabel, hintLabel, rightImageView, topSeparator, bottomSeparator)
- }
- override func layoutSubviews() {
- super.layoutSubviews()
- layout()
- }
- private func layout() {
- selectionView.frame = self.bounds
- if isTopSeparatorVisible {
- topSeparator.pin.top().left(topSeparatorMargin).right().height(separatorHeight)
- }
- if isBottomSeparatorVisible {
- bottomSeparator.pin.bottom().left(bottomSeparatorMargin).right().height(separatorHeight)
- }
- let leftIconSide: CGFloat = leftIcon == nil ? 0 : 24
- leftImageView.pin.left(16).vCenter().width(leftIconSide).height(leftIconSide)
- let rightIconSide: CGFloat = rightIcon == nil ? 0 : 24
- rightImageView.pin.right(16).vCenter().width(rightIconSide).height(rightIconSide)
- hintLabel.pin
- .before(of: rightImageView)
- .marginLeft(8)
- .marginRight(rightIcon == nil ? 0 : 8)
- .vCenter()
- .sizeToFit(.width)
- titleLabel.pin
- .horizontallyBetween(leftImageView, and: hintLabel)
- .top(12)
- .marginLeft(leftIcon == nil ? 0 : 16)
- .sizeToFit(.width)
- }
- override func sizeThatFits(_ size: CGSize) -> CGSize {
- layout()
- return CGSize(width: bounds.size.width, height: titleLabel.frame.maxY + 12)
- }
- override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
- guard self.point(inside: point, with: event) else {
- return nil
- }
- return self
- }
- override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
- isHighlighted = true
- return true
- }
- override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
- guard point(inside: touch.location(in: self), with: event) else {
- isHighlighted = false
- return false
- }
- return true
- }
- override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
- isHighlighted = false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement