detect urls in UILabel - ios

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.

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.

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.

Related

Format UILabel with image and links

How can I add a string with link and image in a UIlabel using swift code.
Below is a sample what I needed:
Visit to the following link Terms and conditions 💼 for specific
instructions.
I need the Terms and conditions and the bag in red collar and remaining text in grey color. Tapping on red coloured text opens a link.
Anyone please help.
The short answer is, don't do that. A UILabel displays a string. That's what it's made to do. Adding images and links to a label is stretching it well beyond its intended purpose.
You can take a generic UIView and add subviews for your text and your image(s). Build your desired contents out of the elements you need.
I would suggest using a UITextView since that supports clickable links. Set it's editable flag to false and turn on link detection.
As per your problem described here. I have found a perfect solution, as per your requirement.
Here is the link to your solution : Link
You can try this and bridge it as it's written in Objective-C
KILabel
I solved it by adding an attributed text and user interaction to handle link tap. To add image NStextAttachment can be used. Write method to handle tap gesture for label using range.

Is it possible to 'hyperlink' text within a UILabel/TextView but activate a segue on tap of this 'hyperlink'?

I say 'hyperlink' because I don't know what else to call it, and that is how i'd like it to appear.
Obviously this is possible using a combination of labels and buttons, but my labels and buttons are programmatically generated and I imagine i'd have to also programmatically arrange them, which would likely be tedious and inflexible in terms of changing font sizes etc.
Any ideas/approaches would be much appreciated!
As an example, look at Instagram's following and news feed:
You should set userInteractionEnabled and then add a UITapGestureRecognizer to the label.
Have a look at Nimbus Attributed Label it can provide the functionality you are looking for.

iOS Format Background

I am creating a simple quote app as a learning experience and need to format it to look better.
I am trying to make the background have a "white square" where the text is and make the text black. Just like the Twitter app has when you click on a tweet.
What is the best way to do this and what should I start with?
I know it is very simple, I'm only 15 and am trying to learn iOS. As you can see I have a server and have the app up and running, just want to format before I call it complete. Thanks! :)
-Henry
This will depend on how you created the label that displays your text, but you can do this very easily:
If you created your UI in a Storyboard / XIB file in XCode: assuming you dragged out a UILabel or UITextView, you can use the Attributes Inspector to set the text color and the background color (in the View section) of the element holding the text.
If you created the label (or text view) in code, you can set the textColor property and the backgroundColor properties programmatically.

How to detect and make hyperlinks/mentions/hashtags clickable in UILabel?

How to detect and make link/mention/hashtag clickable in UILabel. Alternatively, is there any open source library that I can utilize (I already looked at Fancy UILabel which doesn't handle multiline tex, TTAttributedLabel which doesn't handle mention/hashtag)?
There is no way to do so with UILabel in the current iOS...
TTTAttributedLabel will let you style up your label, however for clickable (or rather - tappable) links you should rather either use a UIWebView and style it in such a was as to disguise it as a Label, or, you could get geeky and split your labels up and use a UIButton in the mix, but that's very messy - like a puzzle, only... they don't fit together.
Last option you might have is to overlay a UIButton over a link, but this requires that you know where the link is and since the question was about detecting links etc...
You should really look into UIWebView.
You can achieve this using following library:
https://github.com/SebastienThiebaud/STTweetLabel

How can I put icons inside a UITextField?

I want to create a custom input view, some of whose buttons insert "icons" into the selected UITextField, like the "RSS" or "Reader" icons which sometimes appear on the right hand side of the URL input field in Mobile Safari. I want the icons to be intermingled with the text and for all intents and purposes (like selection, cut and paste and deletion) behave like normal character glyphs.
Is there a name for these icons? Is there an API for creating them or do I have to build this entirely from scratch?
The leftView and rightView properties (instances of UIView) of the UITextField class are exactly for this purpose, and you can control their behaviour using the leftViewMode and rightViewMode properties. I suggest you to read the UITextField class reference for further details.
According to this question, what I want is an NSTokenField and there's not one available in stock iOS. However, there are some implementations on GitHub.

Resources