Reputation: 4543
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
Upvotes: 9
Views: 16375
Reputation: 2580
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.
Upvotes: 11
Reputation: 1839
Use a UITextView, it will parse your text and turn links into blue underlined hyperlinks:
textView.dataDetectorTypes = UIDataDetectorTypeLink;
Upvotes: 2
Reputation: 63
I know this is an old question, but...
Try using NSMutableAttributedString. It has two methods for attributes:
void addAttribute:(NSString *)value range:(NSRange)
and
void addAttributes:(NSDictionary *) range:(NSRange)
Upvotes: 2
Reputation:
Jeremy has published JTextView
, a Core Text-based UITextView
analogue that supports attributed text, including links.
In a nutshell, JTextView:
-drawRect:
, it uses a data detector for links. If a link has been found, it sets its custom attribute for the corresponding rangeUpvotes: 6
Reputation: 47231
Yes it doesn't exist, if you reduce your search to iOS Library it won't give you any results for NSLinkAttributeName
. It's not there...officially, maybe private?
This nice article mentions hyperlinks but does not show an example. I doubt that it's possible with public API methods.
Upvotes: 0