2020-09-30 18:06:47 -07:00
|
|
|
//
|
|
|
|
|
// DocumentControlView.swift
|
|
|
|
|
// App
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 9/30/20.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2021-04-28 17:03:27 -07:00
|
|
|
class DocumentControlItemView: UIControl
|
2020-09-30 18:06:47 -07:00
|
|
|
{
|
|
|
|
|
static public let controlHeight = CGFloat(48.0)
|
|
|
|
|
|
|
|
|
|
let imageView = UIImageView(frame: .zero)
|
|
|
|
|
let label = UILabel(frame: .zero)
|
|
|
|
|
|
2021-07-13 18:11:47 -07:00
|
|
|
enum ViewType: String {
|
|
|
|
|
case imageView
|
|
|
|
|
case label
|
|
|
|
|
case separator
|
|
|
|
|
case highlightView
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 18:06:47 -07:00
|
|
|
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
|
|
|
|
|
|
2021-02-11 12:26:13 -08:00
|
|
|
label.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
|
label.textAlignment = .left
|
2020-09-30 18:06:47 -07:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 18:11:47 -07:00
|
|
|
private func box(_ bounds: CGRect) -> Box<ViewType> {
|
|
|
|
|
Box {
|
|
|
|
|
let padding: CGFloat = 18.0
|
|
|
|
|
let imageSize: CGFloat = 24.0
|
|
|
|
|
let ibounds = bounds.inset(by: layoutMargins)
|
|
|
|
|
|
|
|
|
|
let imageRect = CGRect(
|
|
|
|
|
x: ibounds.minX + 6.0, y: 0.0,
|
|
|
|
|
width: imageSize, height: imageSize
|
|
|
|
|
).centeredY(inRect: bounds)
|
|
|
|
|
|
|
|
|
|
(ViewType.imageView, imageRect)
|
|
|
|
|
|
|
|
|
|
(ViewType.highlightView, bounds)
|
|
|
|
|
|
|
|
|
|
(ViewType.label, CGRect(
|
|
|
|
|
x: imageRect.maxX + padding, y: ibounds.minY,
|
|
|
|
|
width: ibounds.width - imageRect.maxX - padding, height: ibounds.height
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
let separatorHeight: CGFloat = 1.0
|
|
|
|
|
|
|
|
|
|
if drawsBottomSeparator {
|
|
|
|
|
(ViewType.separator, CGRect(
|
|
|
|
|
x: bounds.minX, y: bounds.height - separatorHeight,
|
|
|
|
|
width: bounds.width, height: separatorHeight
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
(ViewType.separator, CGRect.zero)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 18:06:47 -07:00
|
|
|
override func layoutSubviews() {
|
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
2021-07-13 18:11:47 -07:00
|
|
|
box(bounds).fill([
|
|
|
|
|
.imageView : imageView,
|
|
|
|
|
.separator : separatorView,
|
|
|
|
|
.label : label,
|
|
|
|
|
.highlightView : highlightView
|
|
|
|
|
])
|
2020-09-30 18:06:47 -07:00
|
|
|
|
2021-07-13 18:11:47 -07:00
|
|
|
separatorView.isHidden = !drawsBottomSeparator
|
2020-09-30 18:06:47 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 18:27:59 -07:00
|
|
|
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
|
|
|
|
|
super.beginTracking(touch, with: event)
|
|
|
|
|
highlightView.isHidden = false
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
|
|
|
|
|
super.endTracking(touch, with: event)
|
|
|
|
|
highlightView.isHidden = true
|
2020-09-30 18:06:47 -07:00
|
|
|
}
|
2021-07-13 18:11:47 -07:00
|
|
|
|
|
|
|
|
public func title(_ title: String) -> Self {
|
|
|
|
|
self.label.text = title
|
|
|
|
|
return self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func image(_ image: UIImage?) -> Self {
|
|
|
|
|
self.imageView.image = image
|
|
|
|
|
return self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func symbol(_ name: String) -> Self {
|
|
|
|
|
return self.image(UIImage(systemName: name))
|
|
|
|
|
}
|
2020-09-30 18:06:47 -07:00
|
|
|
}
|