diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 4f56c272..c094f775 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -91,16 +91,14 @@ open class Label: UILabel, ViewProtocol, UserInfoable { private struct LabelAction { var range: NSRange var action: PassthroughSubject - var accessibilityId: Int = 0 - + var frame: CGRect = .zero func performAction() { action.send() } - init(range: NSRange, action: PassthroughSubject, accessibilityID: Int = 0) { + init(range: NSRange, action: PassthroughSubject) { self.range = range self.action = action - self.accessibilityId = accessibilityID } } @@ -263,24 +261,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable { 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 //-------------------------------------------------- @@ -373,10 +354,17 @@ open class Label: UILabel, ViewProtocol, UserInfoable { //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) - + let customAccessibilityAction = customAccessibilityElement(text: mutableAttributedString.string, + range: actionable.range, + accessibleText: actionable.accessibleText) + + // creat the action + let labelAction = LabelAction(range: actionable.range, action: actionable.action) + + customAccessibilityAction?.action = labelAction.performAction //create a wrapper for the attributes range, block and - actions.append(LabelAction(range: actionable.range, action: actionable.action, accessibilityID: customAccessibilityAction?.hashValue ?? -1)) + actions.append(labelAction) + isUserInteractionEnabled = true } } @@ -402,7 +390,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable { } } - private func customAccessibilityAction(text: String?, range: NSRange, accessibleText: String? = nil) -> UIAccessibilityCustomAction? { + private func customAccessibilityElement(text: String?, range: NSRange, accessibleText: String? = nil) -> AccessibilityActionElement? { guard let text = text, let attributedText else { return nil } @@ -423,31 +411,13 @@ open class Label: UILabel, ViewProtocol, UserInfoable { let substringBounds = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer) // Create custom accessibility element - let element = UIAccessibilityElement(accessibilityContainer: self) + let element = AccessibilityActionElement(accessibilityContainer: self) element.accessibilityLabel = actionText element.accessibilityTraits = .link + element.accessibilityHint = "Double tap to open" 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 - } - } + return element } } - -