NSAttributedString and Links on iOS
Asked Answered
P

5

9

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

Plaint answered 24/4, 2011 at 19:45 Comment(1)
FYI although this constant isn’t defined publicly, nothing would stop you from reading its value on OS X (it’s NSLink, following the pattern of almost all the other attribute-name constants) and using that for the semantics. Functionally, IIRC, this attribute doesn’t provide anything in CoreText.Peacetime
S
6

Jeremy has published JTextView, a Core Text-based UITextView analogue that supports attributed text, including links.

In a nutshell, JTextView:

  • Creates its own attribute for links by using a custom name for the attribute name and the attribute value contains a representation of the corresponding URL
  • In -drawRect:, it uses a data detector for links. If a link has been found, it sets its custom attribute for the corresponding range
  • In its tap gesture recogniser method, it gets the line frame that contains the tap point. For each glyph run in that line, it gets its attributes and checks whether they contain the custom attribute for links. If so, it opens the URL.
Spasm answered 24/4, 2011 at 23:14 Comment(1)
JTextView link is broken. See here for infoIphlgenia
N
11

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.

Neurophysiology answered 17/12, 2013 at 20:14 Comment(0)
S
6

Jeremy has published JTextView, a Core Text-based UITextView analogue that supports attributed text, including links.

In a nutshell, JTextView:

  • Creates its own attribute for links by using a custom name for the attribute name and the attribute value contains a representation of the corresponding URL
  • In -drawRect:, it uses a data detector for links. If a link has been found, it sets its custom attribute for the corresponding range
  • In its tap gesture recogniser method, it gets the line frame that contains the tap point. For each glyph run in that line, it gets its attributes and checks whether they contain the custom attribute for links. If so, it opens the URL.
Spasm answered 24/4, 2011 at 23:14 Comment(1)
JTextView link is broken. See here for infoIphlgenia
M
2

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)
Manganese answered 6/1, 2012 at 19:18 Comment(0)
C
2

Use a UITextView, it will parse your text and turn links into blue underlined hyperlinks:

textView.dataDetectorTypes = UIDataDetectorTypeLink;
Chuckwalla answered 14/6, 2013 at 16:6 Comment(0)
N
0

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.

Necessity answered 24/4, 2011 at 19:49 Comment(1)
Is there a way to duplicate the results?Plaint

© 2022 - 2024 — McMap. All rights reserved.