iOS 长按选择文本

发布于 2024-11-16 08:19:53 字数 337 浏览 6 评论 0原文

我正在构建一个简单的基于表格的搜索应用程序。用户当然可以通过键盘进行搜索。但我也想有另一种搜索方式。

假设用户在 UILabel长按,然后根据按下发生的位置,应该选择该单词并显示该单词。应该在该单词上进行搜索(无需键盘)。

我知道如何检测 longpress 事件,但有人知道如何复制、检测和保存事件吗?访问发生长按的确切术语?

更新:我知道我可以获取发生longpress的对象,从中我可以获取标签文本的内容。但我需要手指所放置的确切单词。

I am building a simple table based search app. The user can ofcourse search through keyboard. But I also want to have another mode of search.

Say a user longpress on a UILabel then based on where the press is happening, that word should get selected & search should happen (no keyboard) on that word.

I know how to detect longpress events but does anyone know how to copy, detect & access the exact term where the longpress has occurred?

UPDATE: I am aware that I can get the object where longpress occurred from which I can get the content of the label text. But I need the exact word on top of which the finger was placed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的往事 2024-11-23 08:19:53

对于 UILabelUITextView 来说,没有什么容易想到的。如果您愿意使用核心文本自行布置文本,那么您可以很容易地找到触摸附近的单词。这样做的问题是你会失去 UILabel 现有的选择功能,并且必须重新实现它们(或者在 Core Text 中进行“阴影布局”,希望在 UILabel< /code>...这里慢慢陷入疯狂。)

我建议使用标准文本选择机制并将“搜索”添加到 菜单。

Nothing easy comes to mind for UILabel or UITextView. If you're wiling to lay the text out yourself with Core Text, then you can pretty easily find words near the touch. The trouble with that is you'd lose the existing selection features of UILabel and have to reimplement them (or do "shadow layout" in Core Text hoping things will lay out identically in the UILabel... slowly descending into madness here.)

Rather than going down this rabbit hole, I'd recommend using the standard text selection mechanism and add "search" to the menu.

千里故人稀 2024-11-23 08:19:53

只需将 UILongPressGestureRecognizer 放在标签上,然后当手势识别器触发时,您可以访问它来自哪个对象并从那里读取标签值。

myLongPressRecognizer.view 

Just put a UILongPressGestureRecognizer on your label then when the gesture recognizer fires you can access which object it came from and read the label value from there.

myLongPressRecognizer.view 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文