I ended up combining solutions from https://stackoverflow.com/a/44878203/2015332 and https://stackoverflow.com/a/49443814/2015332 (iOS < 11 variant). This works as expected: a read-only, non selectable UITextView on which hyperlinks are still working. One of the advantages from Coeur's solution is that touch detection is immediate and does not display highlight nor allow drag&drop of a link.
Here is the resulting code:
class HyperlinkEnabledReadOnlyTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
isEditable = false
isSelectable = false
initHyperLinkDetection()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
isEditable = false
isSelectable = false
initHyperLinkDetection()
}
// MARK: - Prevent interaction except on hyperlinks
// Combining https://stackoverflow.com/a/44878203/2015332 and https://stackoverflow.com/a/49443814/1033581
private var linkGestureRecognizer: UITapGestureRecognizer!
private func initHyperLinkDetection() {
// Native UITextView links gesture recognizers are broken on iOS 11.0-11.1:
// https://stackoverflow.com/questions/46143868/xcode-9-uitextview-links-no-longer-clickable
// So we add our own UITapGestureRecognizer, which moreover detects taps faster than native one
linkGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped))
linkGestureRecognizer.numberOfTapsRequired = 1
addGestureRecognizer(linkGestureRecognizer)
linkGestureRecognizer.isEnabled = true // because previous call sets it to false
}
override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
// Prevents drag and drop gestures, but also prevents a crash with links on iOS 11.0 and 11.1.
// https://stackoverflow.com/a/49535011/1033581
gestureRecognizer.isEnabled = false
super.addGestureRecognizer(gestureRecognizer)
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
// Allow only taps located over an hyperlink
var location = point
location.x -= textContainerInset.left
location.y -= textContainerInset.top
guard location.x >= bounds.minX, location.x <= bounds.maxX, location.y >= bounds.minY, location.y <= bounds.maxY else { return false }
let charIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
return attributedText.attribute(.link, at: charIndex, effectiveRange: nil) != nil
}
@objc private func textTapped(recognizer: UITapGestureRecognizer) {
guard recognizer == linkGestureRecognizer else { return }
var location = recognizer.location(in: self)
location.x -= textContainerInset.left
location.y -= textContainerInset.top
guard location.x >= bounds.minX, location.x <= bounds.maxX, location.y >= bounds.minY, location.y <= bounds.maxY else { return }
let characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
let characterRange = NSRange(location: characterIndex, length: 1)
if let attachment = attributedText?.attribute(.attachment, at: index, effectiveRange: nil) as? NSTextAttachment {
if #available(iOS 10.0, *) {
_ = delegate?.textView?(self, shouldInteractWith: attachment, in: characterRange, interaction: .invokeDefaultAction)
} else {
_ = delegate?.textView?(self, shouldInteractWith: attachment, in: characterRange)
}
}
if let url = attributedText?.attribute(.link, at: characterIndex, effectiveRange: nil) as? URL {
if #available(iOS 10.0, *) {
_ = delegate?.textView?(self, shouldInteractWith: url, in: characterRange, interaction: .invokeDefaultAction)
} else {
_ = delegate?.textView?(self, shouldInteractWith: url, in: characterRange)
}
}
}
}
Please not that I had some trouble compiling the .attachment
enum case, I removed it because I'm not using it.