193 lines
6.3 KiB
Swift
193 lines
6.3 KiB
Swift
//
|
|
// BaseButton.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 11/22/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import VDSFormControlsTokens
|
|
import Combine
|
|
|
|
/// Base class used for UIButton type classes.
|
|
@objc(VDSButtonBase)
|
|
open class ButtonBase: UIButton, ViewProtocol, UserInfoable, Clickable {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
initialSetup()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Combine Properties
|
|
//--------------------------------------------------
|
|
/// Set of Subscribers for any Publishers for this Control.
|
|
open var subscribers = Set<AnyCancellable>()
|
|
|
|
open var onClickSubscriber: AnyCancellable? {
|
|
willSet {
|
|
if let onClickSubscriber {
|
|
onClickSubscriber.cancel()
|
|
}
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Properties
|
|
//--------------------------------------------------
|
|
private var initialSetupPerformed = false
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
/// Key of whether or not updateView() is called in setNeedsUpdate()
|
|
open var shouldUpdateView: Bool = true
|
|
|
|
open var surface: Surface = .light { didSet { setNeedsUpdate() } }
|
|
|
|
/// Text that will be used in the titleLabel.
|
|
open var text: String? { didSet { setNeedsUpdate() } }
|
|
|
|
/// Array of LabelAttributeModel objects used in rendering the text.
|
|
open var textAttributes: [any LabelAttributeModel]? { nil }
|
|
|
|
/// TextStyle used on the titleLabel.
|
|
open var textStyle: TextStyle { .defaultStyle }
|
|
|
|
/// UIColor used on the titleLabel text.
|
|
open var textColor: UIColor { .black }
|
|
|
|
/// Will determine if a scaled font should be used for the titleLabel font.
|
|
open var useScaledFont: Bool = false { didSet { setNeedsUpdate() } }
|
|
|
|
open var userInfo = [String: Primitive]()
|
|
|
|
/// State of animating isHighlight.
|
|
public var isHighlighting = false
|
|
|
|
/// Whether the Button should handle the isHighlighted state.
|
|
open var shouldHighlight: Bool { isHighlighting == false }
|
|
|
|
/// Whether the Control is highlighted or not.
|
|
open override var isHighlighted: Bool {
|
|
didSet {
|
|
if shouldHighlight {
|
|
isHighlighting = true
|
|
setNeedsUpdate()
|
|
isHighlighting = false
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Whether the Control is enabled or not.
|
|
open override var isEnabled: Bool { didSet { setNeedsUpdate() } }
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
open func initialSetup() {
|
|
if !initialSetupPerformed {
|
|
backgroundColor = .clear
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
accessibilityCustomActions = []
|
|
setup()
|
|
setNeedsUpdate()
|
|
}
|
|
}
|
|
|
|
|
|
open func setup() {
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
titleLabel?.adjustsFontSizeToFitWidth = false
|
|
titleLabel?.lineBreakMode = .byTruncatingTail
|
|
titleLabel?.numberOfLines = 1
|
|
}
|
|
|
|
open func updateView() {
|
|
updateLabel()
|
|
}
|
|
|
|
open func updateAccessibility() {
|
|
if isEnabled {
|
|
accessibilityTraits.remove(.notEnabled)
|
|
} else {
|
|
accessibilityTraits.insert(.notEnabled)
|
|
}
|
|
}
|
|
|
|
open func reset() {
|
|
shouldUpdateView = false
|
|
surface = .light
|
|
isEnabled = true
|
|
text = nil
|
|
accessibilityCustomActions = []
|
|
shouldUpdateView = true
|
|
setNeedsUpdate()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Overrides
|
|
//--------------------------------------------------
|
|
override open var intrinsicContentSize: CGSize {
|
|
let intrinsicContentSize = super.intrinsicContentSize
|
|
let adjustedWidth = intrinsicContentSize.width + titleEdgeInsets.left + titleEdgeInsets.right
|
|
let adjustedHeight = intrinsicContentSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom
|
|
return CGSize(width: adjustedWidth, height: adjustedHeight)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Methods
|
|
//--------------------------------------------------
|
|
private func updateLabel() {
|
|
|
|
//clear the arrays holding actions
|
|
accessibilityCustomActions = []
|
|
|
|
//create the primary string
|
|
let mutableText = NSMutableAttributedString.mutableText(for: text ?? "No Text",
|
|
textStyle: textStyle,
|
|
useScaledFont: useScaledFont,
|
|
textColor: textColor,
|
|
alignment: titleLabel?.textAlignment ?? .center,
|
|
lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail)
|
|
|
|
if let attributes = textAttributes {
|
|
//loop through the models attributes
|
|
for attribute in attributes {
|
|
//add attribute on the string
|
|
attribute.setAttribute(on: mutableText)
|
|
}
|
|
}
|
|
|
|
//set the attributed text
|
|
setAttributedTitle(mutableText, for: .normal)
|
|
setAttributedTitle(mutableText, for: .highlighted)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: AppleGuidelinesTouchable
|
|
extension ButtonBase: AppleGuidelinesTouchable {
|
|
/// Overrides to ensure that the touch point meets a minimum of the minimumTappableArea.
|
|
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
|
Self.acceptablyOutsideBounds(point: point, bounds: bounds)
|
|
}
|
|
|
|
}
|