I have a Tableview and tableview cell is customized to have a UILabel. The text in UILabel is having URLs. Is there a way to detect urls like how UITextView will enable detect URLs so that user interaction should be able to load the urls.
2 Answers
If you just want to identify the URLs, you can use NSDataDetector
with the NSTextCheckingTypeLink
checking type.
If you want to draw the URLs differently, and you are targeting iOS 6, you can use an NSAttributedString
to turn the URLs blue or underline them or whatever. If you're targeting an older version of iOS, you will probably want to look for some free code on the Internet to draw styled text, like OHAttributedLabel
.
If you want to actually make the URLs touch-sensitive, you can add a tap gesture recognizer to the label and try to figure out which part of the string was tapped (somewhat complicated), or look for some free code on the Internet that already does it for you (like OHAttributedLabel
), or just put a UITextView
in the table view cell instead of a label.

- 375,296
- 67
- 796
- 848
-
My [answer here](http://stackoverflow.com/a/29352519/168594) has example code that shows you how to make ranges of your UILabel tap-able with a UITapGestureRecognizer, as @RobMayoff suggested. – zekel Mar 31 '15 at 20:06
as Rob point out how can we achieved the same is awesome.
But, we can use a tact so can save with issue of ios version (possibly), just by using a UILabel and UIButton.
What we need to do is that, either from IB or story-board,just place a UILabel with string as URL(use this as title), say;
"www.myURL.com"
Now, just above it, place a UIButton(button with custom type), and just use "______"(underline) and set this button as overlap your UILabel and also the 'underline' must be beneath your label.
Now, just do in action of button whatever you required as you need on the click of URL and here also you can change the textColor, etc, properties; also load URL and navigate to UIWebView.

- 8,816
- 3
- 44
- 59

- 840
- 6
- 10