updated for label click

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-06-19 10:53:19 -05:00
parent 4aa081f6de
commit c20027dd2c
2 changed files with 48 additions and 3 deletions

View File

@ -176,7 +176,7 @@ open class SelectorItemBase<Selector: SelectorControlable>: Control, Errorable,
/// Executed on initialization for this View.
open override func initialSetup() {
super.initialSetup()
selectorView.onClick = { [weak self] control in
onClick = { [weak self] control in
self?.toggle()
}
}
@ -248,6 +248,20 @@ open class SelectorItemBase<Selector: SelectorControlable>: Control, Errorable,
}
}
/// Overriden to take the hit if there is an onClickSubscriber and the view is not a UIControl
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let labelPoint = convert(point, to: label)
let childLabelPoint = convert(point, to: childLabel)
if label.isAction(for: labelPoint) {
return label
} else if childLabel.isAction(for: childLabelPoint) {
return childLabel
} else {
return super.hitTest(point, with: event)
}
}
/// Resets to default settings.
open override func reset() {
super.reset()

View File

@ -371,7 +371,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
}
if let accessibilityElements, !accessibilityElements.isEmpty {
let staticText = UIAccessibilityElement(accessibilityContainer: self)
let staticText = AccessibilityActionElement(accessibilityContainer: self)
staticText.accessibilityLabel = text
staticText.accessibilityFrameInContainerSpace = bounds
@ -385,13 +385,39 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
@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) {
let location = gesture.location(in: self)
if didTapActionInLabel(location, inRange: actionable.range) {
actionable.performAction()
return
}
}
}
public func isAction(for location: CGPoint) -> Bool {
for actionable in actions {
if didTapActionInLabel(location, inRange: actionable.range) {
return true
}
}
return false
}
private func didTapActionInLabel(_ location: CGPoint, inRange targetRange: NSRange) -> Bool {
guard let attributedText else { return false }
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: bounds.size)
let textStorage = NSTextStorage(attributedString: attributedText)
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
let characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
guard let _ = attributedText.attribute(NSAttributedString.Key.action, at: characterIndex, effectiveRange: nil) as? String, characterIndex < attributedText.length else { return false }
return true
}
private func customAccessibilityElement(text: String?, range: NSRange, accessibleText: String? = nil) -> AccessibilityActionElement? {
guard let text = text, let attributedText else { return nil }
@ -422,4 +448,9 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
accessibilityElements?.append(element)
return element
}
public override func accessibilityActivate() -> Bool {
return false
}
}