如何让 UITextView 将光标设置在最接近拖放点的字符之间?

发布于 2024-12-19 03:52:26 字数 629 浏览 2 评论 0原文

编辑:结果我偶然发现了答案,UITextView必须首先成为第一响应者。

我正在尝试将光标位置设置为最接近拖放操作释放点的位置。这是我到目前为止所得到的,但最后一步似乎什么也没做。

UITextPosition *theTextPosition = [self.tweetTextView closestPositionToPoint:self.dragView.center];
UITextPosition *endPosition = [self.tweetTextView positionFromPosition:theTextPosition
                                                                offset:0];

UITextRange *textRange = [self.tweetTextView textRangeFromPosition:theTextPosition
                                                        toPosition:endPosition];

[self.tweetTextView setSelectedTextRange:textRange];

EDIT: Turns out I stumbled upon the answer, the UITextView has to be first responder first.

I'm trying to set the cursor position to the closest approximation for the release point of a drag and drop operation. Here's what I've got so far, but the last step seems to do nothing.

UITextPosition *theTextPosition = [self.tweetTextView closestPositionToPoint:self.dragView.center];
UITextPosition *endPosition = [self.tweetTextView positionFromPosition:theTextPosition
                                                                offset:0];

UITextRange *textRange = [self.tweetTextView textRangeFromPosition:theTextPosition
                                                        toPosition:endPosition];

[self.tweetTextView setSelectedTextRange:textRange];

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

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

发布评论

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

评论(1

苏佲洛 2024-12-26 03:52:26

原来我偶然发现了答案,UITextView 必须首先成为第一响应者。

Turns out I stumbled upon the answer, the UITextView has to be first responder first.

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