2018-12-13 18:58:50 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
|
2018-12-13 18:58:50 +00:00
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
class ButtonCell: UITableViewCell {
|
2018-12-13 18:58:50 +00:00
|
|
|
var buttonText: String {
|
|
|
|
get { return button.title(for: .normal) ?? "" }
|
|
|
|
set(value) { button.setTitle(value, for: .normal) }
|
|
|
|
}
|
|
|
|
var hasDestructiveAction: Bool {
|
2019-10-14 21:43:56 +00:00
|
|
|
get { return button.tintColor == .systemRed }
|
|
|
|
set(value) { button.tintColor = value ? .systemRed : buttonStandardTintColor }
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
var onTapped: (() -> Void)?
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let button: UIButton = {
|
|
|
|
let button = UIButton(type: .system)
|
|
|
|
button.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
button.titleLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
return button
|
|
|
|
}()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
var buttonStandardTintColor: UIColor
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
buttonStandardTintColor = button.tintColor
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
contentView.addSubview(button)
|
|
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
button.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
|
|
|
|
contentView.layoutMarginsGuide.bottomAnchor.constraint(equalTo: button.bottomAnchor),
|
|
|
|
button.centerXAnchor.constraint(equalTo: contentView.centerXAnchor)
|
2018-12-20 17:22:37 +00:00
|
|
|
])
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
@objc func buttonTapped() {
|
|
|
|
onTapped?()
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
buttonText = ""
|
|
|
|
onTapped = nil
|
|
|
|
hasDestructiveAction = false
|
|
|
|
}
|
|
|
|
}
|