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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With