|
import UIKit |
|
|
|
class BorderButton: UIButton { |
|
|
|
private class BorderLayer: CAShapeLayer { |
|
fileprivate var _borderWidth: CGFloat { UIAccessibility.isBoldTextEnabled ? 2.0 : 1.0 } |
|
fileprivate var _cornerRadius: CGFloat = 4.0 |
|
|
|
override var lineWidth: CGFloat { get { _borderWidth } set { } } |
|
override var fillColor: CGColor? { get { UIColor.clear.cgColor } set { } } |
|
|
|
override var path: CGPath? { get { super.path } set { } } |
|
|
|
fileprivate func updateBorder() { |
|
let rect = CGRect(origin: .zero, size: self.frame.size).insetBy(dx: _borderWidth, dy: _borderWidth) |
|
super.path = UIBezierPath(roundedRect: rect, cornerRadius: _cornerRadius).cgPath |
|
} |
|
} |
|
|
|
override class var layerClass: AnyClass { BorderLayer.self } |
|
private var borderLayer: BorderLayer? { self.layer as? BorderLayer } |
|
|
|
// This is called regardless of how the AXButton is created, IB or code |
|
override func didMoveToSuperview() { |
|
super.didMoveToSuperview() |
|
self.titleLabel?.adjustsFontForContentSizeCategory = true |
|
self.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body) |
|
} |
|
|
|
override func layoutSubviews() { |
|
super.layoutSubviews() |
|
|
|
// Disable CALayer's implicit animations while changing layer properties. |
|
CATransaction.setDisableActions(true) |
|
|
|
// Make its color match the view's tintColor. |
|
// Since we are inside `layoutSubviews()`, UIKit has already set `UITraitCollection.current` |
|
// to be the view's `traitCollection`. Calling `cgColor` will resolve the dynamic tint color, |
|
// using that trait collection, to a static `CGColor`. |
|
// When the traits change, UIKit ensures it calls `layoutSubviews` again. |
|
self.borderLayer?.strokeColor = self.tintColor.cgColor |
|
self.borderLayer?.updateBorder() |
|
|
|
CATransaction.setDisableActions(false) |
|
} |
|
} |