// // UITapGesture.swift // VDS // // Created by Matt Bruce on 8/4/22. // import Foundation import UIKit extension UITapGestureRecognizer { 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 } }