// // PollVoteButton.swift // Tusker // // Created by Shadowfacts on 5/1/22. // Copyright © 2022 Shadowfacts. All rights reserved. // import UIKit /// Wraps a UILabel and UIButton to allow setting disabled titles on Catalyst, where `setTitle(_:for:)` only works for the normal state. class PollVoteButton: UIView { var disabledTitle: String = "" { didSet { update() } } var isEnabled = true { didSet { update() } } private var button = UIButton(type: .system) #if targetEnvironment(macCatalyst) private var label = UILabel() #endif override init(frame: CGRect) { super.init(frame: frame) button.translatesAutoresizingMaskIntoConstraints = false button.setTitleColor(.secondaryLabel, for: .disabled) button.contentHorizontalAlignment = .trailing embedSubview(button) #if targetEnvironment(macCatalyst) label.textColor = .secondaryLabel label.translatesAutoresizingMaskIntoConstraints = false label.textAlignment = .right embedSubview(label) #endif update() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func addTarget(_ target: Any, action: Selector) { button.addTarget(target, action: action, for: .touchUpInside) } func setFont(_ font: UIFont) { button.titleLabel!.font = font #if targetEnvironment(macCatalyst) label.font = font #endif } private func update() { button.isEnabled = isEnabled if isEnabled { #if targetEnvironment(macCatalyst) label.isHidden = true button.isHidden = false #endif button.setTitle("Vote", for: .normal) } else { #if targetEnvironment(macCatalyst) label.text = disabledTitle label.isHidden = false button.isHidden = true #else button.setTitle(disabledTitle, for: .disabled) #endif } } }