From 244e3c0df3146c90f686dd42db58c9ef4acb1cd4 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Wed, 10 Jul 2019 13:15:35 -0400 Subject: [PATCH] Generally functioning. A ways to go. --- MVMCoreUI/Atoms/Views/Label.swift | 61 +++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index 5a189cb5..38fef66f 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -40,7 +40,16 @@ public typealias ActionBlock = () -> Void //------------------------------------------------------ public var clauses: [ActionableClause] = [] { - didSet { isUserInteractionEnabled = !clauses.isEmpty } + didSet { + isUserInteractionEnabled = !clauses.isEmpty + + // Accessibility + let element = UIAccessibilityElement(accessibilityContainer: self) + element.accessibilityFrameInContainerSpace = convert(boundingRect(forCharacterRange: clauses.last!.range!), to: nil) //CGRect(x: 10, y: 10, width: 100, height: 30) + element.accessibilityLabel = "Testing" + element.accessibilityTraits = .button + accessibilityElements?.append(element) + } } /// Used for tappable links in the text. @@ -64,10 +73,7 @@ public typealias ActionBlock = () -> Void lineBreakMode = .byWordWrapping translatesAutoresizingMaskIntoConstraints = false isAccessibilityElement = false - - let tapGesture = UITapGestureRecognizer(target: self, action: #selector(textLinkTapped(_:))) - tapGesture.numberOfTapsRequired = 1 - addGestureRecognizer(tapGesture) + accessibilityElements = [] } @objc public init() { @@ -410,6 +416,7 @@ extension Label { originalAttributedString = nil hasAttachmentImage = false styleB2(true) + accessibilityElements = [] } @objc public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { @@ -450,6 +457,7 @@ extension Label { } self.attributedText = mutableAttributedString +// accessibleClauses = [] clauses = [] } @@ -567,38 +575,36 @@ extension UITapGestureRecognizer { extension Label { override open func accessibilityActivate() -> Bool { - let point = accessibilityActivationPoint - +// let point = accessibilityActivationPoint return true } open override func accessibilityElementDidBecomeFocused() { - <#code#> + } open override func accessibilityElementCount() -> Int { - return accessibleClauses.count + return accessibilityElements?.count ?? 0 } open override func accessibilityElement(at index: Int) -> Any? { - return accessibleClauses[index] + return accessibilityElements?[index] } open override func index(ofAccessibilityElement element: Any) -> Int { - return accessibleClauses.firstIndex(of: element as! UIAccessibilityElement)! + return 0//accessibilityElements?.firstIndex(of: element as! UIAccessibilityElement)! } + /* var accessibleClauses: [UIAccessibilityElement] { var elements = [UIAccessibilityElement]() - for clause in clauses { - let element = UIAccessibilityElement(accessibilityContainer: self) - let rect = CGRect(x: 10, y: 10, width: 100, height: 30) - element.accessibilityFrame = rect +// let rect = CGRect(x: 10, y: 10, width: 100, height: 30) + element.accessibilityFrame = convert(self.frame, to: nil) // element.accessibilityFrame = convert(conceptualSize(range: clause.range)!, to: nil) - element.accessibilityFrameInContainerSpace = rect +// element.accessibilityFrameInContainerSpace = rect element.accessibilityLabel = "Testing" element.accessibilityTraits = .button element.isAccessibilityElement = true @@ -607,4 +613,27 @@ extension Label { accessibilityElements = elements return elements } + */ + + func boundingRect(forCharacterRange range: NSRange) -> CGRect? { + + guard let attributedText = attributedText else { return nil } + + let textStorage = NSTextStorage(attributedString: attributedText) + let layoutManager = NSLayoutManager() + + textStorage.addLayoutManager(layoutManager) + + let textContainer = NSTextContainer(size: bounds.size) + textContainer.lineFragmentPadding = 0.0 + + layoutManager.addTextContainer(textContainer) + + var glyphRange = NSRange() + + // Convert the range for glyphs. + layoutManager.characterRange(forGlyphRange: range, actualGlyphRange: &glyphRange) + + return layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer) + } }