|
import PlaygroundSupport |
|
import UIKit |
|
|
|
final class DeluxeButton: UIView { |
|
fileprivate let imageView: UIImageView = { |
|
let imageView = UIImageView() |
|
imageView.contentMode = .scaleAspectFit |
|
return imageView |
|
}() |
|
|
|
fileprivate let label: UILabel = { |
|
let label = UILabel() |
|
label.font = UIFont.systemFont( |
|
ofSize: 40, |
|
weight: UIFontWeightHeavy) |
|
label.textAlignment = .center |
|
label.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) |
|
label.adjustsFontSizeToFitWidth = true |
|
return label |
|
}() |
|
|
|
fileprivate lazy var stackView: UIStackView = { |
|
let stackView = UIStackView(arrangedSubviews: [self.imageView, self.label]) |
|
stackView.axis = .vertical |
|
stackView.translatesAutoresizingMaskIntoConstraints = false |
|
return stackView |
|
}() |
|
|
|
public required init?(coder aDecoder: NSCoder) { |
|
super.init(coder: aDecoder) |
|
initPhase2() |
|
} |
|
|
|
public override init(frame: CGRect) { |
|
super.init(frame: frame) |
|
initPhase2() |
|
} |
|
|
|
private func initPhase2() { |
|
label.backgroundColor = tintColor |
|
layer.borderColor = tintColor.cgColor |
|
|
|
layer.cornerRadius = 20 |
|
layer.borderWidth = 10 |
|
|
|
addSubview(stackView) |
|
|
|
NSLayoutConstraint.activate([ |
|
stackView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor), |
|
stackView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor), |
|
stackView.leftAnchor.constraint(equalTo: layoutMarginsGuide.leftAnchor), |
|
stackView.rightAnchor.constraint(equalTo: layoutMarginsGuide.rightAnchor), |
|
label.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.3) |
|
]) |
|
} |
|
|
|
override func tintColorDidChange() { |
|
label.backgroundColor = tintColor |
|
layer.borderColor = tintColor.cgColor |
|
} |
|
} |
|
|
|
// Public API |
|
extension DeluxeButton { |
|
var image: UIImage? { |
|
get { |
|
return imageView.image |
|
} |
|
set { |
|
imageView.image = newValue?.withRenderingMode(.alwaysTemplate) |
|
} |
|
} |
|
|
|
var text: String? { |
|
get { |
|
return label.text |
|
} |
|
set { |
|
label.text = newValue |
|
} |
|
} |
|
} |
|
|
|
let dimensions = (width: 200, height: 200) |
|
let deluxeButton = DeluxeButton (frame: CGRect(x: dimensions.width/2, |
|
y: dimensions.height/2, |
|
width: dimensions.width, |
|
height: dimensions.height)) |
|
|
|
deluxeButton.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) |
|
deluxeButton.tintColor = #colorLiteral(red: 0.06274510175, green: 0, blue: 0.1921568662, alpha: 1) |
|
deluxeButton.text = "GITHUB" |
|
deluxeButton.image = #imageLiteral(resourceName: "github-icon.png") |
|
|
|
let view = UIView(frame: deluxeButton.frame.insetBy(dx: -100, dy: -100)) |
|
view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) |
|
view.addSubview(deluxeButton) |
|
|
|
PlaygroundPage.current.liveView = view |