Last active
December 8, 2019 00:25
-
-
Save saoudrizwan/986714d5a093f481fb3f4f6589418ea6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkResponsiveTextView: UITextView { | |
override init(frame: CGRect, textContainer: NSTextContainer?) { | |
super.init(frame: frame, textContainer: textContainer) | |
self.delaysContentTouches = false | |
// required for tap to pass through on to superview & for links to work | |
self.isScrollEnabled = false | |
self.isEditable = false | |
self.isUserInteractionEnabled = true | |
self.isSelectable = true | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
// location of the tap | |
var location = point | |
location.x -= self.textContainerInset.left | |
location.y -= self.textContainerInset.top | |
// find the character that's been tapped | |
let characterIndex = self.layoutManager.characterIndex(for: location, in: self.textContainer, fractionOfDistanceBetweenInsertionPoints: nil) | |
if characterIndex < self.textStorage.length { | |
// if the character is a link, handle the tap as UITextView normally would | |
if (self.textStorage.attribute(NSLinkAttributeName, at: characterIndex, effectiveRange: nil) != nil) { | |
return self | |
} | |
} | |
// otherwise return nil so the tap goes on to the next receiver | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @saoudrizwan I'm getting "Fatal error: init(coder:) has not been implemented:" any idea on how or where to implement that so that I get this class working? Thank you!!
I was able to fix the issue by replacing your required init with this:
required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }