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

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?

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.

Related

Have UITextView make first line current cursor position?

Long story short I need to get a UITextView to automatically scroll to the current cursor line, however I need one additional thing. Instead of just having the TextView be scrolled "in view", I need the cursor line to be in view specifically as the first line of the textView.
Any ideas how to accomplish this?
Thanks.
[UITextView -selectedRange] will return the insertion point as the location member
[UITextView -scrollRangeToVisible:] will scroll to the specified range
Combining the two will enable you to scroll to the current cursor line.
Based on your comment, it sounds like you most likely want to adjust the frame of the text view when the keyboard is shown.
Apple provides some nice notifications:
UIWindow Class Reference - Notifications
UIWindow Class Reference - User Info keys
Make sure to check the supported operating system version as iOS 5.0 brought some new, and better, notifications and user info dictionaries

Focus and zoom in on a UITextField when touched?

How in the world does one get the iPhone view to zoom in on a focused UITextField? I need to tap on a text field that I want to edit, and my view should zoom in to the tapped text field and pull up the keyboard (which it already does), similar to how many Internet text fields gain focus on the iPhone. Is this some type of overlay?
I've been looking everywhere for this solution but maybe I've just got the wrong terminology. Thank you in advance.
Could be a duplicate of this StackOverflow question.
In essence, there are a number of ways, but you have to program this effect manually. A textfield is already an overlay. You can either move it, or scroll the containing view.
Please follow the following steps.
Implement the delegate method for all textfield.connect the outlet of textfield in interface builder basically it's setting the delegate property.then in delegate property you can defined the method whatever you want to implement or wanted to do functionality.
Thanks

Best way to allow text edit completion on a complex iPhone UI

I have a somewhat complex iOS view hierarchy. One piece of text is an editable UITextField. When the user touches it, it becomes first responder, and is editable.
Here's the rub, though: Best practice should be that a touch anywhere outside the edit control causes it to resign first responder and end editing. What's the best way of accomplishing this?
Techniques I've tried:
Use the exclusiveTouch property, which stops the user from interacting with other controls, but doesn't cause editing to end. Also disallows user from interacting with my toolbar "Done" button.
Put a see-through UIView under the text field control and on top of everything else (except the toolbar), and use touches there to end editing. This works, but I end up reparenting the text field onto this other random view which sits above my whole hierarchy, which means I have to take care of the text field's layout in multiple places, since it no longer lives in the place where it lived originally, and I have to delegate all its behavior back and forth from its "shield" view to its native home container, which has all the related logic.
Is there an elegant solution to this problem that I'm missing? I figure it must be a common design issue.
Thanks.
Tile 4 "see-thru" views around the textview to capture/ignore touches. Doesn't require modifying or "lifting" the textview, and can be added to the parent view in a fairly modular way.
You can't mask a region without knowing what that mask will cover and what the mask will not cover. So any solution will require enough reach to gather both of those bounds. Either pass the text rect up, or the view rect/region to be disabled down, or both to something in-between. The controller for the stuff to be covered seems as good a place as any to consolidate both rects or regions, if not the controller for the text view.
The nub of the issue is what constitutes "best practice". The fact that the keyboard remains unless the user dismisses it is deliberate. For example, many apps need the user to be able to tap a button while still working in a text field.
The keyboard has a Return button. "Best practice" is to respond to the user tapping that button by resigning first responder. Otherwise, you should leave the keyboard there, since that's what the user expects.
However, if you insist on doing it your way, there's a simple solution: put a UITapGestureRecognizer on the background view. Its handler will be triggered if the user taps on the background or on any button or similar in the interface. So, presuming you have kept a record of what the first responder is, you can send resignFirstResponder to the first responder in the tap gesture recognizer's handler.
If you change your base view to a UIControl you can add an IBAction to that layer that resigns your text field as first responder.
Also, if you have multiple touch events, make sure they each becomeFirstResponder when touched.
I'd love to have some more details to qualify my explanations xD

Custom Number Pad UIKeyboard

How would I go about creating a custom number pad like what is used in 'Tipulator' and other apps.
I know how you can customize UIKeyboard, but their number pad doesn't look at all like the default number pad. Is it even a UIKeyboard or a separate UIView?
I've been working with keyboards for quite a time. I would say that the easiness of this depends on what your target text input view is. If is a UITextView then we are fine, if is UITextField you might have some problems because you don't have access to current cursor text position like un UITextView.
(You might check UITextView and UITextViewDelegate methods)
If you just want to set a string and don't mind current cursor text position, then you don't need a keyboard. (I think this is the case of Tipulator)
BTW: I just saw Tipulator in youtube and there is no necessity of a keyboard for doing that.
Judging by the looks: It's a custom UIView subclass. Basically, it's just a panel with 11 buttons on it, so it should be rather easy to do.

How do you make a UITextView that supports URL links and can be edited?

I have a UITextView that is properly displaying URLs thusly:
contentView.editable = NO;
contentView.dataDetectorTypes = UIDataDetectorTypeAll;
My goal is to make it so you can still tap on this text view in order to edit its text at a particular location (just like the built-in Notes app). That way, if you tap a link, it'll launch a browser, but if you tap anywhere else, it'll start editing at the point where you tapped. Should be easy, right?
Not so far. Subclassing the UITextView and overriding touchesEnded gives you a chance to set editable to YES. But when you do that, the text view doesn't remember where you tapped (the selectedRange doesn't get set properly), so editing always begins at the bottom of the text view.
I've even tried using the undocumented setSelectionWithPoint method, but it doesn't behave as you'd expect.
Can anyone think of some way to achieve a proper tap-to-edit UITextView with tappable links?
Perhaps you could try either retaining the UITouch and UIEvent or forging copies using custom classes that have the same class signatures, then re-send the touchesBegan: and touchesEnded: events after setting editable to YES.