1

I have a simple NSString, for example :

NSString *text = @"Stackoverflow is amazing!"

and I'd like to turn the word Stackoverflow into a hyperlink pointing to https://stackoverflow.com/ and still be able to output the string as a variable. Is this possible?

Community
  • 1
  • 1
user1419810
  • 836
  • 1
  • 16
  • 29
  • 1
    Refer here : http://stackoverflow.com/questions/21629784/make-a-clickable-link-in-an-nsattributedstring-for-a-uitextfield-or-uilabel and here : http://stackoverflow.com/questions/8839464/uilabel-string-as-text-and-links – Sushil Sharma Oct 14 '15 at 09:18

1 Answers1

7

A string only contains letters - no formatting what so ever.

To make a part into a link, you have to Attributed Text and assign the first word a NSLink attribute with the url:

NSURL *url = [NSURL URLWithString:@"http://www.google.de"];

NSAttributedString *my = [NSAttributedString attributedStringWithString:@"my"
                                                             attributes:@{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont systemFontWithSize:16]}];
NSAttributedString *link = [NSAttributedString attributedStringWithString:@"Link"
                                                               attributes:@{NSForegroundColorAttributeName:[UIColor blue], NSFontAttributeName:[UIFont systemFontWithSize:16], NSLinkAttributeName:url}];
NSMutableAttributedString *attr = [my mutableCopy];
[attr appendAttributedString:link];

show it in a textview, a UILabel doesn't support clicking AFAIK

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135