Goofing around with Box

This commit is contained in:
James Magahern
2021-07-13 18:11:47 -07:00
parent 560b1a4a75
commit 777b079f5e
5 changed files with 133 additions and 48 deletions

View File

@@ -14,6 +14,13 @@ class DocumentControlItemView: UIControl
let imageView = UIImageView(frame: .zero)
let label = UILabel(frame: .zero)
enum ViewType: String {
case imageView
case label
case separator
case highlightView
}
var drawsBottomSeparator: Bool = false {
didSet { setNeedsLayout() }
}
@@ -50,38 +57,68 @@ class DocumentControlItemView: UIControl
CGSize(width: size.width, height: Self.controlHeight)
}
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)
}
}
}
override func layoutSubviews() {
super.layoutSubviews()
highlightView.frame = bounds
box(bounds).fill([
.imageView : imageView,
.separator : separatorView,
.label : label,
.highlightView : highlightView
])
let padding: CGFloat = 18.0
let imageSize: CGFloat = 24.0
let bounds = self.bounds.inset(by: layoutMargins)
imageView.frame = CGRect(
x: bounds.minX + 6.0, 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
}
separatorView.isHidden = !drawsBottomSeparator
}
override func setTracking(_ tracking: Bool) {
super.setTracking(tracking)
highlightView.isHidden = !tracking
}
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))
}
}