iPhone SDK:如何创建一个在点击位置插入文本的 UITextView?

发布于 2024-08-31 23:34:15 字数 202 浏览 4 评论 0原文

我想创建一个 UITextView,您可以点击其中的任何位置并开始在该位置键入。该控件的默认行为是从最后一个字符结束处开始键入。因此,如果我有一个没有文本的 UITextView,并点击控件的中间,我想从那里开始输入,而不是从左上角开始。

实施这种行为的最佳方法是什么?我考虑过将视图的默认文本值设置为 3000 个空格字符或类似的值,但这似乎不是一个优雅的解决方案。建议?

I'd like to create a UITextView that you can tap anywhere within it and start typing at that location. The default behavior of the control is that typing starts where the last character ended. So, if I had a UITextView with no text in it and tap in the middle of the control, I'd like typing to start there--not in the upper left.

What is the best way to implement this behavior? I've considered making the default text value of the view to be 3000 space characters or something similar, but this seems like not an elegant solution. Suggestions?

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

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

发布评论

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

评论(1

空城缀染半城烟沙 2024-09-07 23:34:15

我建议从 UITextView 派生来创建一个处理点击的自定义视图。您可能需要重写以下方法:

  • touchesBegan:withEvent
  • touchesMoved:withEvent
  • touchesEnded:withEvent
  • touchesCancelled:withEvent code>

确保 userInteractionEnabled 属性的默认值为 YES。覆盖 hitTest:withEventpointInside:withEvent 以找出用户在视图中点击的位置。

请务必阅读iOS View 编程指南中的响应事件部分,另请参阅 iOS 事件处理指南了解更多详细信息。

无论如何,一旦您弄清楚用户触摸的位置,您就可以根据需要修改文本或重新定位克拉。

I suggest deriving from UITextView to create a custom view that handles taps. You'll want to override the following methods, probably:

  • touchesBegan:withEvent
  • touchesMoved:withEvent
  • touchesEnded:withEvent
  • touchesCancelled:withEvent

Make sure the userInteractionEnabled property has a default value of YES. Override hitTest:withEvent and pointInside:withEvent to figure out where in your view the user tapped.

Be sure and read the Responding to Events section in the View Programming Guide for iOS, and also see the Event Handling Guide for iOS for more details.

Anyway, once you figure out where the user touched, you can modify the text or reposition the karat as appropriate.

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