|
| 1 | +// |
| 2 | +// Toggle.swift |
| 3 | +// DesignSystem |
| 4 | +// |
| 5 | +// Created by ttozzi on 7/31/25. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | + |
| 10 | +public final class Toggle: UISwitch { |
| 11 | + |
| 12 | + override public var isEnabled: Bool { |
| 13 | + didSet { |
| 14 | + updateUI(isEnabled) |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + public override init(frame: CGRect) { |
| 19 | + super.init(frame: frame) |
| 20 | + setupUI() |
| 21 | + } |
| 22 | + |
| 23 | + required init?(coder: NSCoder) { |
| 24 | + fatalError() |
| 25 | + } |
| 26 | + |
| 27 | + private func setupUI() { |
| 28 | + onTintColor = STColors.primary1.color |
| 29 | + snp.makeConstraints { make in |
| 30 | + make.width.equalTo(51) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private func updateUI(_ isEnabled: Bool) { |
| 35 | + if isEnabled { |
| 36 | + onTintColor = STColors.primary1.color |
| 37 | + } else { |
| 38 | + onTintColor = STColors.primary9.color |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +@available(iOS 17.0, *) |
| 44 | +#Preview { |
| 45 | + let stackView = UIStackView().then { |
| 46 | + $0.axis = .vertical |
| 47 | + $0.spacing = 20 |
| 48 | + $0.alignment = .leading |
| 49 | + } |
| 50 | + let activeStackView = UIStackView().then { |
| 51 | + $0.axis = .horizontal |
| 52 | + $0.spacing = 20 |
| 53 | + $0.alignment = .center |
| 54 | + } |
| 55 | + let activeDefaultLabel = UILabel().then { |
| 56 | + $0.text = "Default" |
| 57 | + $0.style = Typography.Body_14_M |
| 58 | + } |
| 59 | + let activeDefaultToggle = Toggle().then { |
| 60 | + $0.isOn = true |
| 61 | + $0.isEnabled = true |
| 62 | + } |
| 63 | + let activeDisabledLabel = UILabel().then { |
| 64 | + $0.text = "Disabled" |
| 65 | + $0.style = Typography.Body_14_M |
| 66 | + } |
| 67 | + let activeDisabledToggle = Toggle().then { |
| 68 | + $0.isOn = true |
| 69 | + $0.isEnabled = false |
| 70 | + } |
| 71 | + activeStackView.addArrangedSubview(activeDefaultLabel) |
| 72 | + activeStackView.addArrangedSubview(activeDefaultToggle) |
| 73 | + activeStackView.addArrangedSubview(activeDisabledLabel) |
| 74 | + activeStackView.addArrangedSubview(activeDisabledToggle) |
| 75 | + |
| 76 | + let inactiveStackView = UIStackView().then { |
| 77 | + $0.axis = .horizontal |
| 78 | + $0.spacing = 20 |
| 79 | + $0.alignment = .center |
| 80 | + } |
| 81 | + let inactiveDefaultLabel = UILabel().then { |
| 82 | + $0.text = "Default" |
| 83 | + $0.style = Typography.Body_14_M |
| 84 | + } |
| 85 | + let inactiveDefaultToggle = Toggle().then { |
| 86 | + $0.isOn = false |
| 87 | + $0.isEnabled = true |
| 88 | + } |
| 89 | + let inactiveDisabledLabel = UILabel().then { |
| 90 | + $0.text = "Disabled" |
| 91 | + $0.style = Typography.Body_14_M |
| 92 | + } |
| 93 | + let inactiveDisabledToggle = Toggle().then { |
| 94 | + $0.isOn = false |
| 95 | + $0.isEnabled = false |
| 96 | + } |
| 97 | + |
| 98 | + inactiveStackView.addArrangedSubview(inactiveDefaultLabel) |
| 99 | + inactiveStackView.addArrangedSubview(inactiveDefaultToggle) |
| 100 | + inactiveStackView.addArrangedSubview(inactiveDisabledLabel) |
| 101 | + inactiveStackView.addArrangedSubview(inactiveDisabledToggle) |
| 102 | + |
| 103 | + let activeTitleLabel = UILabel().then { |
| 104 | + $0.text = "Control/Toggle/Active" |
| 105 | + $0.style = Typography.Heading_20_B |
| 106 | + } |
| 107 | + let inactiveTitleLabel = UILabel().then { |
| 108 | + $0.text = "Control/Toggle/Inactive" |
| 109 | + $0.style = Typography.Heading_20_B |
| 110 | + } |
| 111 | + stackView.addArrangedSubview(activeTitleLabel) |
| 112 | + stackView.addArrangedSubview(activeStackView) |
| 113 | + stackView.addArrangedSubview(inactiveTitleLabel) |
| 114 | + stackView.addArrangedSubview(inactiveStackView) |
| 115 | + |
| 116 | + return stackView |
| 117 | +} |
0 commit comments