// // DocumentControlView.swift // App // // Created by James Magahern on 9/30/20. // import UIKit class DocumentControlView: UIControl { static public let controlHeight = CGFloat(48.0) let imageView = UIImageView(frame: .zero) let label = UILabel(frame: .zero) var drawsBottomSeparator: Bool = false { didSet { setNeedsLayout() } } internal let highlightView = UIView(frame: .zero) internal let separatorView = UIView(frame: .zero) init() { super.init(frame: .zero) addSubview(highlightView) addSubview(imageView) addSubview(label) addSubview(separatorView) tintColor = .label label.font = UIFont.preferredFont(forTextStyle: .subheadline) label.textAlignment = .center imageView.contentMode = .center separatorView.backgroundColor = .secondarySystemFill highlightView.backgroundColor = .secondarySystemFill highlightView.isHidden = true } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func sizeThatFits(_ size: CGSize) -> CGSize { CGSize(width: size.width, height: Self.controlHeight) } override func layoutSubviews() { super.layoutSubviews() highlightView.frame = bounds let padding: CGFloat = 18.0 let imageSize: CGFloat = 24.0 let bounds = self.bounds.inset(by: layoutMargins) imageView.frame = CGRect( x: bounds.minX, y: 0.0, width: imageSize, height: imageSize ).centeredY(inRect: self.bounds) label.frame = CGRect( x: imageView.frame.maxX + padding, y: bounds.minY, width: bounds.width - imageView.frame.maxX - padding, height: bounds.height ) let separatorHeight: CGFloat = 1.0 if drawsBottomSeparator { separatorView.isHidden = false separatorView.frame = CGRect( x: self.bounds.minX, y: self.bounds.height - separatorHeight, width: self.bounds.width, height: separatorHeight ) } else { separatorView.isHidden = true } } override func setTracking(_ tracking: Bool) { super.setTracking(tracking) highlightView.isHidden = !tracking } }