SetupButton.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. @IBDesignable class SetupButton: UIButton {
  22. @IBInspectable var cancelStyle: Bool = false {
  23. didSet {
  24. setup()
  25. }
  26. }
  27. @IBInspectable var accentColor: UIColor = Colors.mainThemeDark() {
  28. didSet {
  29. setup()
  30. }
  31. }
  32. @IBInspectable var textColor: UIColor = Colors.white() {
  33. didSet {
  34. setup()
  35. }
  36. }
  37. var deactivated: Bool {
  38. set {
  39. self.isEnabled = !newValue
  40. setup()
  41. }
  42. get {
  43. return self.isEnabled
  44. }
  45. }
  46. override init(frame: CGRect) {
  47. super.init(frame: frame)
  48. setup()
  49. }
  50. required init?(coder aDecoder: NSCoder) {
  51. super.init(coder: aDecoder)
  52. setup()
  53. }
  54. private func setup() {
  55. self.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
  56. self.alpha = self.isEnabled ? 1.0 : 0.5
  57. self.isUserInteractionEnabled = self.isEnabled
  58. self.layer.cornerRadius = 3
  59. self.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 36))
  60. // calculate disabled color
  61. var red : CGFloat = 0
  62. var green : CGFloat = 0
  63. var blue : CGFloat = 0
  64. self.accentColor.getRed(&red, green: &green, blue: &blue, alpha: nil)
  65. let accentColorDisabled: UIColor = UIColor(red: red, green: green, blue: blue, alpha: 0.5)
  66. if self.cancelStyle {
  67. self.backgroundColor = .clear
  68. self.setTitleColor(self.accentColor, for: .normal)
  69. self.layer.borderWidth = 1
  70. self.layer.borderColor = self.isEnabled ? self.accentColor.cgColor : accentColorDisabled.cgColor
  71. } else {
  72. self.backgroundColor = self.isEnabled ? self.accentColor : accentColorDisabled
  73. self.setTitleColor(self.textColor, for: .normal)
  74. }
  75. }
  76. }