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

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.

Related

OHAttributedLabel and UITableView

I am using OHAttributedLabel to create a text link inside my UITableViewCell. However, when I am tapping this link, I am having hard time getting the tap through, it seems like I have to hold my finger a lot longer to get OHAttributedLabel to realize that I want to tap the link.
I do not have this problem outside UITableViews, everything works perfectly. Curiously, this same error happens also in tableViewFooter and header. I have set userInteractionEnabled = YES for the label, and NO for the cell. What could I be doing wrong here?

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

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 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.

Auto-scroll UITextView after text append if scrollbar is at bottom

I am continuously appending text to the content of my UITextView and would like to auto-scroll to the latest update, only if if the UITextView has already been scrolled to the bottom. Any ideas on how this could be implemented?
UITextView inherits from UIScrollView, so you can therefore access all of the same properties as with a UIScrollView. Personally, I would use a combination of
myScrollView.contentOffset.y
to detect whether or not the text is scrolled all the way down, and
[myScrollView zoomToRect:CGRectMake(x,y,height,width) animated:YES]
to shift the current view to the newly added text.
See the docs for UITextView and UIScrollView. If you didn't know already, those can be accessed from XCode by clicking Help>>Developer Documentation
Good Luck!
James
To all those who want to automatically scroll a uitextview when a keyboard appears/disappears, search and download keyboardaccessory.xcodeproj by apple.
Although the program tests in an ipad simulator, you can copy the parts of the code and paste them in yours.
Specifically:
Copy the code on listening for the 2 notifications for keyboardWillShow and keyboardWillHide in the viewDidLoad.
Copy the code releasing the notification in dealloc.
Remove notification listening status in viewDidDisappear.
Copy the codes in keyboardWillShow, keyboardWillHide to programmatically compute for the height of the keyboard and scroll the text automatically.
Remove the keyboard in textViewShouldEndEditing by calling [textView resignFirstResponder];
If you want this implemented in both portrait and landscape, the code above will already do the necessary computations. all you have to do is allow rotation in shouldRotateToInterfaceOrientation

Highlite Select Text in UIWebview

i want to Select text using gesture and highlite that text.i had done display html page in UIwebview but uiwebview does not fire touch begin event, So how can i do.
1) when user select text (i.e user select text "i Know uesr" that part of text color should be change.
if you have any idea below of the query,kindly suggest me..
Thank you
Milan
If you look at the docs for a UIWebView, you know your options are limited.
You're going to have do it with javascript. Your only weapon is - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script and your ability to load every page as a string (which you can manipulate) before passing it to you UIWebView as a page (using - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL).
So... good luck with that. ;)
Brute force method
You can trap the touches as the Window level by subclassing UIWindow. Then you can manually locate the view the touch occurred in and then the location in the view.
As to actually highlighting the text with the OS, I think you might have to put a transparent overlay view on top of the webview and then draw the highlight there.
You're going to have trouble selecting in a webview because a single touch also opens links. You're going to have to create an interface that distinguishes between link touches and selection touches.