// // VDSLabel.swift // VDS // // Created by Matt Bruce on 7/28/22. // import Foundation import UIKit import VDSCoreTokens import Combine /// Label is a standard view used to draw text with applying Typography through ``TextStyle`` as well /// as other attributes using any implemetation of ``LabelAttributeModel``. @objc(VDSLabel) open class Label: UILabel, ViewProtocol, UserInfoable { //-------------------------------------------------- // 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() //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private enum TextSetMode { case text case attributedText } private var textSetMode: TextSetMode = .text private var initialSetupPerformed = false private var edgeInsets: UIEdgeInsets { textStyle.edgeInsets } private var tapGesture: UITapGestureRecognizer? { willSet { if let tapGesture = tapGesture, newValue == nil { removeGestureRecognizer(tapGesture) } else if let gesture = newValue, tapGesture == nil { addGestureRecognizer(gesture) } } } private var actions: [LabelAction] = [] { didSet { isUserInteractionEnabled = !actions.isEmpty if actions.isEmpty { tapGesture = nil } else { //add tap gesture if tapGesture == nil { let singleTap = UITapGestureRecognizer(target: self, action: #selector(textLinkTapped)) singleTap.numberOfTapsRequired = 1 tapGesture = singleTap } if actions.count > 1 { actions.sort { first, second in return first.range.location < second.range.location } } } } } //-------------------------------------------------- // MARK: - Private Models //-------------------------------------------------- private struct LabelAction { var range: NSRange var action: PassthroughSubject var accessibilityId: Int = 0 func performAction() { action.send() } init(range: NSRange, action: PassthroughSubject, accessibilityID: Int = 0) { self.range = range self.action = action self.accessibilityId = accessibilityID } } //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- /// Key of whether or not updateView() is called in setNeedsUpdate() open var shouldUpdateView: Bool = true /// Will determine if a scaled font should be used for the font. open var useScaledFont: Bool = false { didSet { setNeedsUpdate() }} open var surface: Surface = .light { didSet { setNeedsUpdate() }} /// Array of LabelAttributeModel objects used in rendering the text. open var attributes: [any LabelAttributeModel]? { didSet { setNeedsUpdate() }} /// TextStyle used on the this label. open var textStyle: TextStyle = .defaultStyle { didSet { setNeedsUpdate() }} /// The alignment of the text within the label. open override var textAlignment: NSTextAlignment { didSet { setNeedsUpdate() }} open var userInfo = [String: Primitive]() /// Number of lines the label can render out, default is set to 0. open override var numberOfLines: Int { didSet { setNeedsUpdate() }} /// Line break mode for the label, default is set to word wrapping. open override var lineBreakMode: NSLineBreakMode { didSet { setNeedsUpdate() }} /// Text that will be used in the label. private var _text: String! override open var text: String! { didSet { _text = text textSetMode = .text setNeedsUpdate() } } ///AttributedText that will be used in the label. override open var attributedText: NSAttributedString? { didSet { textSetMode = .attributedText setNeedsUpdate() } } override open var font: UIFont! { didSet { if let font, initialSetupPerformed { textStyle = TextStyle.convert(font: font) } setNeedsUpdate() } } override open var textColor: UIColor! { didSet { if let textColor, initialSetupPerformed { textColorConfiguration = SurfaceColorConfiguration(textColor, textColor).eraseToAnyColorable() } setNeedsUpdate() } } /// Whether the View is enabled or not. /// Since the UILabel itselfs draws a different color for the "disabled state", I have to track /// local variable to deal with color and always enforce this UILabel is always enabled. private var _fakeIsEnabled: Bool = true open override var isEnabled: Bool { get { true } set { _fakeIsEnabled = newValue setNeedsUpdate() } } //-------------------------------------------------- // MARK: - Configuration Properties //-------------------------------------------------- /// Color configuration use for text color. This is a configuration that will provide a color based /// of local properties such as surface and isEnabled. open var textColorConfiguration: AnyColorable = ViewColorConfiguration().with { $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forDisabled: true) $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false) }.eraseToAnyColorable(){ didSet { setNeedsUpdate() }} //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- open func initialSetup() { if !initialSetupPerformed { initialSetupPerformed = true //register for ContentSizeChanges NotificationCenter .Publisher(center: .default, name: UIContentSizeCategory.didChangeNotification) .sink { [weak self] notification in self?.setNeedsUpdate() }.store(in: &subscribers) backgroundColor = .clear numberOfLines = 0 lineBreakMode = .byTruncatingTail translatesAutoresizingMaskIntoConstraints = false accessibilityCustomActions = [] isAccessibilityElement = true accessibilityTraits = .staticText textAlignment = .left setup() setNeedsUpdate() } } open func setup() {} open func reset() { shouldUpdateView = false surface = .light isEnabled = true attributes = nil textStyle = .defaultStyle textAlignment = .left text = nil attributedText = nil numberOfLines = 0 backgroundColor = .clear shouldUpdateView = true setNeedsUpdate() } open func updateView() { restyleText() //force a drawText setNeedsDisplay() setNeedsLayout() } open func updateAccessibility() { accessibilityLabel = text if isEnabled { accessibilityTraits.remove(.notEnabled) } else { accessibilityTraits.insert(.notEnabled) } } //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- /// We are drawing using edgeInsets based off the textStyle. open override func drawText(in rect: CGRect) { super.drawText(in: rect.inset(by: edgeInsets)) } /// We are applying action attributes after the views layout to ensure correct positioning of the text. open override func layoutSubviews() { super.layoutSubviews() applyActions() } /// Addig custom accessibillty actions from the collection of attributes. open override func accessibilityActivate() -> Bool { guard let accessibleActions = accessibilityCustomActions else { return false } for actionable in actions { for action in accessibleActions { if action.hash == actionable.accessibilityId { actionable.performAction() return true } } } return false } //-------------------------------------------------- // MARK: - Private Methods //-------------------------------------------------- private func restyleText() { if textSetMode == .text { styleText(_text) } else { styleAttributedText(attributedText) } } private struct FakeEnabled: Enabling, Surfaceable { var surface: Surface var isEnabled: Bool } /// Var to deal with the UILabel.isEnabled property causing issues with /// textColor when it is false, I am now using a struct to draw and manage /// colors instead of this class itself and this class will always be enabled private var _textColor: UIColor { let fake = FakeEnabled(surface: surface, isEnabled: _fakeIsEnabled) return textColorConfiguration.getColor(fake) } private func styleText(_ newValue: String!) { defer { invalidateIntrinsicContentSize() } guard let newValue, !newValue.isEmpty else { // We don't need to use attributed text super.attributedText = nil super.text = newValue return } //clear out accessibility accessibilityElements?.removeAll() accessibilityCustomActions = [] //create the primary string let mutableText = NSMutableAttributedString.mutableText(for: newValue, textStyle: textStyle, useScaledFont: useScaledFont, textColor: _textColor, alignment: textAlignment, lineBreakMode: lineBreakMode) applyAttributes(mutableText) // Set attributed text to match typography super.attributedText = mutableText } private func styleAttributedText(_ newValue: NSAttributedString?) { defer { invalidateIntrinsicContentSize() } guard let newValue, !newValue.string.isEmpty else { // We don't need any additional styling super.attributedText = newValue return } //clear out accessibility accessibilityElements?.removeAll() accessibilityCustomActions = [] let mutableText = NSMutableAttributedString(attributedString: newValue) applyAttributes(mutableText) // Modify attributed text to match typography super.attributedText = mutableText } private func applyAttributes(_ mutableAttributedString: NSMutableAttributedString) { actions = [] if let attributes { mutableAttributedString.apply(attributes: attributes) } } private func applyActions() { actions = [] guard let attributedText else { return } let mutableAttributedString = NSMutableAttributedString(attributedString: attributedText) if let attributes { //loop through the models attributes for attribute in attributes { //see if the attribute is Actionable if let actionable = attribute as? any ActionLabelAttributeModel, mutableAttributedString.isValid(range: actionable.range) { //create a accessibleAction let customAccessibilityAction = customAccessibilityAction(text: mutableAttributedString.string, range: actionable.range, accessibleText: actionable.accessibleText) //create a wrapper for the attributes range, block and actions.append(LabelAction(range: actionable.range, action: actionable.action, accessibilityID: customAccessibilityAction?.hashValue ?? -1)) } } if let accessibilityElements, !accessibilityElements.isEmpty { let staticText = UIAccessibilityElement(accessibilityContainer: self) staticText.accessibilityLabel = text staticText.accessibilityFrameInContainerSpace = bounds isAccessibilityElement = false self.accessibilityElements = accessibilityElements.compactMap{$0 as? UIAccessibilityElement}.filter { $0.accessibilityLabel != text } self.accessibilityElements?.insert(staticText, at: 0) } } } @objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) { for actionable in actions { // This determines if we tapped on the desired range of text. if gesture.didTapActionInLabel(self, inRange: actionable.range) { actionable.performAction() return } } } private func customAccessibilityAction(text: String?, range: NSRange, accessibleText: String? = nil) -> UIAccessibilityCustomAction? { guard let text = text, let attributedText else { return nil } let actionText = accessibleText ?? (text.isValid(range: range) ? NSString(string:text).substring(with: range) : text) // Calculate the frame of the substring let layoutManager = NSLayoutManager() let textContainer = NSTextContainer(size: bounds.size) let textStorage = NSTextStorage(attributedString: attributedText) layoutManager.addTextContainer(textContainer) textStorage.addLayoutManager(layoutManager) var glyphRange = NSRange() // Convert the range for the substring into a range of glyphs layoutManager.characterRange(forGlyphRange: range, actualGlyphRange: &glyphRange) let substringBounds = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer) // Create custom accessibility element let element = UIAccessibilityElement(accessibilityContainer: self) element.accessibilityLabel = actionText element.accessibilityTraits = .link element.accessibilityFrameInContainerSpace = substringBounds //TODO: accessibilityHint for Label // element.accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "swipe_to_select_with_action_hint") accessibilityElements = (accessibilityElements ?? []).compactMap{$0 as? UIAccessibilityElement}.filter { $0.accessibilityLabel != actionText } accessibilityElements?.append(element) let accessibleAction = UIAccessibilityCustomAction(name: actionText, target: self, selector: #selector(accessibilityCustomAction(_:))) accessibilityCustomActions?.append(accessibleAction) return accessibleAction } @objc private func accessibilityCustomAction(_ action: UIAccessibilityCustomAction) { for actionable in actions { if action.hash == actionable.accessibilityId { actionable.performAction() return } } } }