2

I want to add a link to some text. At the moment the text is in a UILabel. I want the link to be on the text so the user would not visually see the link. How can this be done?

In web development it looks like this.

<a href="//example.com">Example 1</a>
Hemang
  • 26,840
  • 19
  • 119
  • 186
Nathan Schafer
  • 273
  • 4
  • 15

2 Answers2

4

UILabel is not made for that, but you can link the text with an action (like tapping) in order to do whatever you like. Don't know about swift but in Objective-C is something like:

// If you have UILabel* myLabel
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
[myLabel addGestureRecognizer:gr];
gr.numberOfTapsRequired = 1;
gr.cancelsTouchesInView = NO;

And then you can add the action:

- (void) myAction: (UITapGestureRecognizer *) gr {
// Code here
}

Maybe you can figure out how to do this in swift

pablopunk
  • 371
  • 1
  • 11
  • 1
    You said UILabel is not made for that. Is there something else that is. – Nathan Schafer Jan 17 '15 at 01:43
  • @NathanSchafer I've never done something like this but it seems you can do it with UITextView, see this link -> http://stackoverflow.com/questions/19854506/uilabel-and-nslinkattributename-link-is-not-clickable – pablopunk Jan 17 '15 at 01:46
  • And there's a project that makes UILabels more capables to do things like this https://github.com/TTTAttributedLabel/TTTAttributedLabel – pablopunk Jan 17 '15 at 01:53
0

You can also make a button look invisible. This could create the effect you are looking for.

Andy Dewan
  • 245
  • 1
  • 2
  • 18