-1
我正在努力將下面的代碼轉換爲抓取在UIWebView中平移過來的字詞。它在UITextView中效果很好。但是,我找不到任何可以在UIWebView中執行的操作。我知道它必須存在,因爲我可以長時間按下UIWebView中加載的網站中的一個詞,並將其選中。我希望能夠以編程方式返回這個詞。最終,我想在ePub或PDF中執行相同的操作。任何幫助表示讚賞。UIWebview字詞識別器
@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
}
這可能工作。我會嘗試一些測試。我對swift很新,因此可能需要我花一點時間來弄清楚如何實現它。 –
這會選擇文字。我只需要返回這個詞。我如何關閉代碼的選擇部分? –
這也依賴於javascript。我有一個JavaScript解決方案,這是不工作我發佈在這裏http://stackoverflow.com/questions/43578361/swift-javascript-word-capture行動是平底鍋手勢,這將返回個人平移的話。雖然這種擴展很接近,但似乎比我需要的更復雜。通過選擇器識別單詞是一個好主意,但是我希望能夠利用選擇器中的代碼部分知道在單詞突出顯示之前選擇單個單詞,並彈出UIMenuViewControl。那有意義嗎? –