Displaying a URL in a UITableViewCell in a different color from the rest of the text - iphone

I'm working through the Stanford iPhone programming course online. The Presence app assignment pulls Twitter and displays each one in a separate UITableViewCell.
Updates often include URL's and I'd like to know how to display just the URL text in blue, having it be tappable. I can parse the text for URL's with no problem, just no idea how to display the URL itself.
Any advice or suggestions would be greatly appreciated.

As said above a button would work, but in terms of the UI it would probably look more natural if you had a label.
Alternatively, you could fill the UITableViewCell with a UIWebView. The UIWebView would hold all the text and you should easily be able to set part of the text as a link (color blue) using html.

You'll have to add a UIButton (or something similar) to the cell, with the button title being the URL and the action opening the actual link.

Related

What's the easiest way to put a URL link into a UITableViewCell

I have some text in a UITableViewCell that has a URL link to it. I'd like to make the URL blue and, when clicked, open up the Safari Browser. Some of suggested that I use a UIWebView within the UITableViewCell, but I've not seen a simple implementation of that. I don't care about scrolling as I only have one row in this table (it is a row that takes up the whole screen as I have lot of text in it). Anyone have any code for the cellForRowAtIndexPath method on how to implement this?
This is clearly a very bad place to use a tableview. That major point aside, you need to give us some more information. Does your cell contain more than one URL? Is(are) the URL(s) mixed in with non link text?
Honestly if you only have one cell, frankly, I see no reason to not just use a UIWebView.

How the Text readjusts when an image appears?

In some news applications when it loads it shows a list of articles. When we click on an article, it goes to its detailed page. In the detailed page 1st there will be only the text related to the article. But suddenly an image comes(related to the article) on the top left corner and the text re-aligns itself to contain the image.
Wht are they doing here? Are they still using UILabel? If i am just adding a UIImageView inside a UILabel, the text will be be adjusted even before the image appears.How can I replicate this myself?
I might be wrong, but i think what they are doing probably similar to the following steps:
User clicks on Article
Article description is being loaded, async download of image is being initiated
once the article description is downloaded the detailsview gets added on top of the navigationstack and appears.
user reads the text....
at some point the download of the image finishes, the viewcontroller gets notified in some way and does a re-layout of the detailsview.
the re-layouting (or however you want to call it) will do two things (probably in an animated fashion)
As I'm not aware how to wrap text around images within a UILabel (keen to get ahold of that knowlegde tough) I would suggest, that what they do is simply creating a second label with the Imageview in place and then fading the first one (text only) out while fading the second one (containing text and image) at the same time..
not sure if this helps you, but I hope so.
cheers
TGiF sam
Use UIWebView, the readjustment you are talking about is html basic property, download your content in html format or make a html file programmatic once you download text and image and show them in UIWebView.

How to Make URLs clickable inside of UITableViewCell?

I know how to use UIWebView and can invoke a WebView if it is associated with a specific button or UITableViewCell. What I am trying to achieve is to have a UITableViewCell with a chunk of text. That chunk of text might contain a URL. I want to make the URL into a clickable link and have that link open into a WebView.
My thought so far has been that I need to detect a link and insert it as a UIButton within the cell... but I don't know if that is the right way to go about this.
Any ideas or input would be much appreciated.
Have a look at the TTLink class in combination with TTTableControlCell of the excellent Three20 project.

Is there a way to create custom UIDataDetectorTypes?

What I am trying to do is create tooltip functionality so that certain words in my instructional app can be tapped and the definition pops up. For the popup part I plan on using code from “AFInformationView” which provides bubbles on the iPhone.
The part I'm struggling with is how to associate A particular word's location with the bubble. Currently I have the text on a UILabel that is on a custom UITableCell. Since I calculate the row height on the fly with:
[textToUse sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(stop-start, 500)];
I'm not sure what the coordinates for a specific word will be. I was thinking that if I created a custom DataDetectorType that could be the fix.
If anyone knows how to do this or has any other ideas I would be happy to hear them.
Thanks,
Andrew
I didn't create a custom UIDataDetectorTypes but Craig Hockenberry did something like it with his TwitterrificTouch.
He uses regular expressions to detect links and other things. I provide it with my keywords and then they become tappable. He places buttons on top of the matching text from the underlying labels. You can google a lot of posts that talk about "putting transparent buttons on top" of various things but Craig's code is the only example/working code I could find.
Here is the link:
http://furbo.org/2008/10/07/fancy-uilabels/
I don't think this is possible. The (few) Data Detector types that the iPhone currently supports are hard-coded with a integer type id. There does not seem to be a mechanism to extends that list of types.
File a feature request in their bug tracker. I will do the same.
AFAIK, you can't create custom data detectors.
The best approach for this sort of thing seems to be using UIWebViews. At least that's what I did. However, you shouldn't use a UIWebView inside a UITableViewCell. In fact, no subview of a UITableViewCell should respond to user input. So I think the best approach would be to display a UIWebView when the cell is tapped.
UIWebViews could be a possible approach but on scrolling you should consider that the whole text should be parsed to detect the words.You could use HTMl tags to make them blue and provide the links.But how could i then assign a custom behavior then opening in safari?
If you want custom data detector you could write an extractor method to primarly patch the links with help of NSregularExpression. For example
NSString regex = #"(http|https|fb)://((\w)|([0-9]*)|([-|_]))+(\.|/)"; to patch alll the links including Facebook URLs inside text like fb://friends.
Then you could use NSattributedString yo mark the links with different colors etc.
ThreeTwenty has a great library called TTTAttributedLabel where you could assign links to certain parts of a text. I also scrolls quite fast if you use it in tableviews
https://github.com/mattt/TTTAttributedLabel

Best approach for adding non-web links to UITextView

I am creating a dictionary-style app that presents a tableview full of words, and when a word is selected, a UITextView is displayed that shows the definition of the word. What I would like to do is add a line that says "See also: synonym1, synonym2" where synonym1 and synonym2 are links that will take the user to the definition for the synonym that is touched.
What is the best way to add these dynamic links? Buttons? Somehow add a small UIWebView a UItable on the fly?
Thanks!
I would replace your UITextView with a UIWebView and use that contain your description text and your links. It's fairly trivial to generate HTML on the fly for something like that.
You could register a custom URL scheme for your app, or you could intercept links as they're clicked.
If your links are always grouped together there's no reason why you couldn't use a bunch of UIButtons inside a custom view, but then you'd have to handle layout and wrapping on your own. It seems a lot easier to do it in HTML.