开发者

iPhone SDK: How to create a UITextView that inserts text where you tap?

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 sim开发者_开发百科ilar, but this seems like not an elegant solution. Suggestions?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜