// // UITapGesture.swift // VDS // // Created by Matt Bruce on 8/4/22. // import Foundation import UIKit extension UITapGestureRecognizer { /// Determines if the touch event has a action attribute within the range given /// - Parameters: /// - label: UILabel in question /// - targetRange: Range to look within /// - Returns: Wether the range in the label has an action public func didTapActionInLabel(_ label: UILabel, inRange targetRange: NSRange) -> Bool { guard let attributedText = label.attributedText else { return false } let layoutManager = NSLayoutManager() let textContainer = NSTextContainer(size: label.bounds.size) let textStorage = NSTextStorage(attributedString: attributedText) layoutManager.addTextContainer(textContainer) textStorage.addLayoutManager(layoutManager) let location = location(in: label) 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 } }