【发布时间】:2017-04-23 04:01:39
【问题描述】:
我正在努力转换下面的代码以获取 UIWebView 中平移的单词。它在 UITextView 中效果很好。但是,我在 UIWebView 中找不到任何可以做到的东西。我知道它一定存在,因为我可以在加载到 UIWebView 的网站中长按一个单词并选择它。我想以编程方式返回这个词。最终,我想在 ePub 或 PDF 中执行相同的操作。任何帮助表示赞赏。
@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
}
【问题讨论】: