I have this UILabel
I agree to the Terms and Policy
. I wish to make Terms and Policy clickable respectively. I have managed to add attribute to Terms and Policy to make them Blue and Underline. But how to make them clickable respectively. When Terms are clicked, I wish to push to a new VC
that loads up the webView and same goes for Policy. I am familiar with using segue
to push to another VC but how to initialise the process when Terms or Policy is clicked>
Edited
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize label;
@synthesize layoutManager;
@synthesize textContainer;
@synthesize textStorage;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullString = @"I agree to the Terms and Policy";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullString];
//For underline
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[fullString rangeOfString:@"Terms"]];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[fullString rangeOfString:@"Policy"]];
//For Blue Colour
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0] range:[fullString rangeOfString:@"Terms"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0] range:[fullString rangeOfString:@"Policy"]];
// Setting attributed string to textview
label.attributedText = attributedString;
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeZero];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
//=====What does this part do, Do I really need it?======
// Configure layoutManager and textStorage
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
// Configure textContainer
textContainer.lineFragmentPadding = 0.0;
textContainer.lineBreakMode = label.lineBreakMode;
textContainer.maximumNumberOfLines = label.numberOfLines;
//======================================================
label.userInteractionEnabled = YES;
[label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnLabel:)]];
}
- (void)handleTapOnLabel:(UITapGestureRecognizer *)tapGesture
{
CGPoint locationOfTouchInLabel = [tapGesture locationInView:tapGesture.view];
CGSize labelSize = tapGesture.view.bounds.size;
CGRect textBoundingBox = [self.layoutManager usedRectForTextContainer:self.textContainer];
CGPoint textContainerOffset = CGPointMake((labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
(labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y);
CGPoint locationOfTouchInTextContainer = CGPointMake(locationOfTouchInLabel.x - textContainerOffset.x,
locationOfTouchInLabel.y - textContainerOffset.y);
NSInteger indexOfCharacter = [self.layoutManager characterIndexForPoint:locationOfTouchInTextContainer
inTextContainer:self.textContainerfractionOfDistanceBetweenInsertionPoints:nil];
NSRange termsLinkRange = NSMakeRange(15, 5); // it's better to save the range somewhere when it was originally used for marking link in attributed string
NSRange policyLinkRange = NSMakeRange(25, 6);
//=========THE FOLLOWING PART IS NOT WORKING =====================
if (NSLocationInRange(indexOfCharacter, termsLinkRange)) {
NSLog(@"This is terms");
[self performSegueWithIdentifier:@"Terms" sender:self];
}else if(NSLocationInRange(indexOfCharacter, policyLinkRange)){
NSLog(@"This is policy");
[self performSegueWithIdentifier:@"Policy" sender:self];
}
}
//======================================================================
@end