I am working to convert the below code to grab the word panned over in a UIWebView. It works great in UITextView. However, I can't find anything which will do it in UIWebView. I know it must exist because I can long press a word in a website loaded in UIWebView and it gets selected. I would like to have the word returned programatically. Ultimately I would like to do this same action in an ePub or PDF. Any help is appreciated.
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let textInView = "Baa baa black sheep, have you any wool? Yes sir, yes sir, three bags full! One for the master, one for the dame, And one for the little boy who lives down the lane."
textView.text = textInView
textView.isEditable=false
textView.isSelectable=false
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panResponse(sender:)))
panGesture.minimumNumberOfTouches = 1
textView.addGestureRecognizer(panGesture)
}
func panResponse(sender: UIPanGestureRecognizer) {
let location: CGPoint = sender.location(in: textView)
let tapPosition: UITextPosition? = textView.closestPosition(to: location)
if tapPosition != nil {
let textRange: UITextRange? = textView.tokenizer.rangeEnclosingPosition(tapPosition!, with: UITextGranularity.word, inDirection: 1)
if textRange != nil {
//Do Something
}