updated to use new accessibility

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-06-19 09:38:34 -05:00
parent 6d42ec599c
commit 4b06ef2e31

View File

@ -91,16 +91,14 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
private struct LabelAction { private struct LabelAction {
var range: NSRange var range: NSRange
var action: PassthroughSubject<Void, Never> var action: PassthroughSubject<Void, Never>
var accessibilityId: Int = 0 var frame: CGRect = .zero
func performAction() { func performAction() {
action.send() action.send()
} }
init(range: NSRange, action: PassthroughSubject<Void, Never>, accessibilityID: Int = 0) { init(range: NSRange, action: PassthroughSubject<Void, Never>) {
self.range = range self.range = range
self.action = action self.action = action
self.accessibilityId = accessibilityID
} }
} }
@ -265,24 +263,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
super.layoutSubviews() super.layoutSubviews()
applyActions() 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 // MARK: - Private Methods
//-------------------------------------------------- //--------------------------------------------------
@ -375,10 +356,17 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
//see if the attribute is Actionable //see if the attribute is Actionable
if let actionable = attribute as? any ActionLabelAttributeModel, mutableAttributedString.isValid(range: actionable.range) { if let actionable = attribute as? any ActionLabelAttributeModel, mutableAttributedString.isValid(range: actionable.range) {
//create a accessibleAction //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 //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
} }
} }
@ -404,7 +392,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 } guard let text = text, let attributedText else { return nil }
@ -425,31 +413,13 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
let substringBounds = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer) let substringBounds = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer)
// Create custom accessibility element // Create custom accessibility element
let element = UIAccessibilityElement(accessibilityContainer: self) let element = AccessibilityActionElement(accessibilityContainer: self)
element.accessibilityLabel = actionText element.accessibilityLabel = actionText
element.accessibilityTraits = .link element.accessibilityTraits = .link
element.accessibilityHint = "Double tap to open"
element.accessibilityFrameInContainerSpace = substringBounds 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 = (accessibilityElements ?? []).compactMap{$0 as? UIAccessibilityElement}.filter { $0.accessibilityLabel != actionText }
accessibilityElements?.append(element) accessibilityElements?.append(element)
return 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
}
}
} }
} }