Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString and Links on iOS

I am trying to add a hyperlink to certain parts of my text which is being handled and drawn using CoreText.

According to Apple's docs on CoreText I should be using addAttribute:NSLinkAttributeName however on iOS (4.3) it says it doesn't know NSLinkAttributeName. In my document searches it looks like NSLinkAttributeName only still exists on the Mac.

Is it available on iOS and I am just missing something? If it's not available how can I create a hyperlink on part of a text using NSMutableAttributedString and CoreText?

Thanks

like image 300
kdbdallas Avatar asked Apr 24 '11 19:04

kdbdallas


People also ask

How do I make a text clickable in Swift?

To make UILabel clickable in Swift, you will need to create a UITapGestureRecognizer and then add it to a label. In this tutorial, you will learn how to create UILabel in Swift programmatically and how to make it clickable by creating and adding to it a UITapGestureRecognizer.

What is NSAttributedString?

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.

What is TTTAttributedLabel?

TTTAttributedLabel is a drop-in replacement for UILabel providing a simple way to performantly render attributed strings. As a bonus, it also supports link embedding, both automatically with NSTextCheckingTypes and manually by specifying a range for a URL, address, phone number, event, or transit information.


1 Answers

As of iOS 7, UITextView supports links in attributed strings:

textView.attributedText = [[NSAttributedString alloc] initWithString:@"Stack Overflow" attributes:@{NSLinkAttributeName: @"http://stackoverflow.com"}];

By default, links are rendered as blue text. You can change the style with UITextView's linkTextAttributes property.

If you set editable to NO on the UITextView, links become tappable.

like image 152
jonahb Avatar answered Oct 20 '22 01:10

jonahb