Can you please tell how i can make each part of the Underlined text clickable.I have a requirement of Terms and Policy where in if user clicks on Terms some API is called and when clicks policy some other API is called.In story board I tried to set a transparent button but While setting constraints its some how not appropriate for all screen sizes like 5s ,Plus sizes and iPAD. Please give some suggestion to solve this issue.
Asked
Active
Viewed 2,127 times
0
-
2Using buttons is the correct approach. Work your way around those constraints. – Tejas K May 16 '17 at 10:06
-
I am not sure if two links is possible since the gesture recognizer is added to the label as whole and attributed text is just another property. In any case let us know what solution you find for your problem. – Shubham Naik May 16 '17 at 10:07
-
1Perhaps you can use WebView and links, this seems the easyest way for me – FredericP May 16 '17 at 10:07
-
One way is you can have two labels close to each other (adjust the constraints accordingly) and add tap gestures to the labels. – Shubham Naik May 16 '17 at 10:10
-
Using Example of `UITextView`: http://stackoverflow.com/questions/2454067/display-html-text-in-uitextview/20996085#20996085 – Imad Ali May 16 '17 at 10:14
-
check [FRHyperLabel](https://github.com/null09264/FRHyperLabel) & [TTTAttributedLabel](https://github.com/TTTAttributedLabel/TTTAttributedLabel) – ChintaN -Maddy- Ramani May 16 '17 at 10:22
-
@TejasK Yes i finally ended up adding a parent view to my label and button hence constraints worked fine with the view and buttons were in place on all device screens. Its working fine now.Thanks – Vishaka Watal May 17 '17 at 10:57
-
@VishakaWatal : Great, you can see my answer here just to get an insight on how you can do it without the parent view : https://stackoverflow.com/questions/26458858/how-to-use-autolayout-to-scale-view-for-all-screen-sizes/43680052#43680052 – Tejas K May 31 '17 at 17:52
3 Answers
2
Do not use buttons as that is a horrible approach. Try the below code. I am assuming you are familiar with adding attributes to the strings.
NSString *mLongText = @"You long text with Terms and Policy keyword in it";
NSRange termsRange = [mLongText rangeOfString:@"Terms"];
NSRange policyRange = [mLongText rangeOfString:@"Policy"];
NSMutableAttributedString *infoString = [[NSMutableAttributedString alloc] initWithString:mLongText];
[infoString addAttribute: NSLinkAttributeName value: @"http://www.yourTermsApi.com" range: termsRange];
[infoString addAttribute: NSLinkAttributeName value: @"http://www.yourPolicyApi.com" range: policyRange];
self.label.attributedText = infoString
As you may have figured out it will add the link attribute to the keywords Terms
and Policy
. So when you tap, the associated api will be called. Add different colors if you want using the above ranges to make the attibutes look visually appealing.

Eray Alparslan
- 806
- 7
- 12

Rishab
- 1,901
- 2
- 18
- 34
1
You could have a label like this with tap gesture and all
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 160, 200, 24)];
[lbl setText:@"Terms and conditions"];
[lbl setUserInteractionEnabled:YES];
[self.view addSubview:lbl];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tncLabel:)];
[lbl addGestureRecognizer:tap];
}
And find the touch location in the Label.
-(void) tncLabel:(UITapGestureRecognizer *) sender
{
CGPoint pt = [sender locationInView:lbl];
NSLog(@"Tap point x: %f", pt.x);
}
Later you could use - (CGRect)rectForSubstringWithRange:(NSRange)range;
to find which part of text was tapped on.
Haven't tried the last part myself. Hope it helps.

Shubham Naik
- 410
- 1
- 7
- 18