首页 > 解决方案 > 如何在 UITextView 中对点击的单词执行操作?

问题描述

我是 Swift 的新手,正在尝试实现一种方法,其中在 textView 中点击的单词(在我的代码中称为“文本”)会导致执行操作。

我在 WWDC 2013 ( https://developer.apple.com/videos/play/wwdc2013/220/ - @24:05) 上观看了演示文稿,并尝试在我的应用程序中实现“命中测试”的示例。但是,我从我的 handleTap 方法收到错误“无法识别的选择器发送到实例”。这是我到目前为止的代码。感谢任何关于我哪里出错的指示。谢谢。

@IBAction func handleTap(sender: 
UITapGestureRecognizer) {

var layoutManager : NSLayoutManager
var location : CGPoint
var characterIndex : Int = 0
var fraction : CGFloat = 0.0

    layoutManager = text.layoutManager
    location = sender.location(in: text)
    characterIndex = layoutManager.characterIndex(for: location, in: text.textContainer, fractionOfDistanceBetweenInsertionPoints: &fraction)

    if (characterIndex < text.textStorage.length) {
        print("tap success!")
    }

标签: swift

解决方案


推荐阅读