iPhone SDK:如何创建一个UITextView,在您点按的位置插入文本? [英] iPhone SDK: How to create a UITextView that inserts text where you tap?

查看:123
本文介绍了iPhone SDK:如何创建一个UITextView,在您点按的位置插入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

推荐答案

我建议从UITextView派生来创建一个处理水龙头的自定义视图。您可能希望覆盖以下方法:

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

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

确保 userInteractionEnabled 属性的默认值为YES。覆盖 hitTest:withEvent pointInside: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.

请务必阅读响应事件部分,另请参阅 iOS事件处理指南了解更多详情。

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.

这篇关于iPhone SDK:如何创建一个UITextView,在您点按的位置插入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆