iPhone SDK:如何创建一个在点击位置插入文本的 UITextView?
我想创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议从 UITextView 派生来创建一个处理点击的自定义视图。您可能需要重写以下方法:
touchesBegan:withEvent
touchesMoved:withEvent
touchesEnded:withEvent
touchesCancelled:withEvent
code>确保
userInteractionEnabled
属性的默认值为 YES。覆盖hitTest:withEvent
和pointInside: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. OverridehitTest:withEvent
andpointInside: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.