-1

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
 }
Clay Smith
  • 189
  • 1
  • 5
  • 14

1 Answers1

0

Hey you can add UIMenuViewController and override the copy option. I made a small demo with a UIWebview extension here is my entire code

extension UIWebView {
  override open var canBecomeFirstResponder: Bool {
    return true 
  }

  override open func copy(_ sender: Any?) {
    let board = UIPasteboard.general
    let selectedString = self.stringByEvaluatingJavaScript(from: "window.getSelection().toString()")
    board.string = selectedString
    let menu = UIMenuController.shared
    menu.setMenuVisible(false, animated: true)
  }

  override open func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(UIResponderStandardEditActions.copy(_:)) {
      return true
    }
    return false
  }
}

I'm just loading a web page in webview. You can just add this extension and the rest should work :).

rameez
  • 374
  • 2
  • 11
  • This might work. I'll try some tests. I'm pretty new to swift so it may take me a bit to figure out how to implement it. – Clay Smith Apr 27 '17 at 01:34
  • This selects text. I just need to return the word. How can I turn off the selection part of the code? – Clay Smith Apr 27 '17 at 01:42
  • This also relies on javascript. I have a javascript solution which is not working I posted here http://stackoverflow.com/questions/43578361/swift-javascript-word-capture The action is the pan gesture which will return individuals words panned over. While this extension is close it seems more complicated than I need. The identifying of the word through the selector is a good thought however I want to be able to utilize the part of the code in the selector which knows to select the single word before the word is highlighted and the UIMenuViewControl pops up. Does that make sense? – Clay Smith Apr 27 '17 at 02:32
  • But why would you not want to show selection it will only create ambiguity. Let me check though how can we do that? – rameez Apr 27 '17 at 07:11
  • As the pangesture happens the words are captured. The s le toon will confuse the process. – Clay Smith Apr 27 '17 at 10:46
  • Any further ideas here? When I long press a word the word gets highlighted and I get the menu to copy it. Where would I find the code detecting the long press? It's there that the system identifies the word start and end pressed on. But I want to return the word not highlight it and I'd like to do it with a pan gesture. – Clay Smith May 09 '17 at 11:39
  • I haven't tried it out but check out this [link](http://stackoverflow.com/questions/25465274/get-tapped-word-from-uitextview-in-swift) – rameez May 10 '17 at 04:54
  • Unfortunately that link is only UItextview. It utilizes methods specific to that class. UIWebview does not have the same methods. :/ – Clay Smith May 13 '17 at 01:49