swift - 如何在 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!")
}
解决方案
推荐阅读
- c++ - 警告:在 C++ 中将字节流读入双变量时,左移计数 >= 类型宽度
- cdn - 策划的 WebAssembly CDN
- amazon-web-services - 最优ETL流程和平台
- html - 有人可以解释这个 HTML 下拉菜单代码吗
- excel - 如何在excel中检查数字是否以“+”开头?
- javascript - 在谷歌地图 JavaScript API 中每 5 秒刷新一次标记位置
- php - 如何使用 Spout 读取上传的文件 ($_FILE)
- elixir - Phoenix/Elixir:何时创建新上下文的提示
- gitlab - gitlab-ci 每周只运行 1 行
- python - 多索引“get_level_values”-任意多个级别的函数