Skip to content

Instantly share code, notes, and snippets.

@Sunnyztj
Created October 30, 2014 02:44
Show Gist options
  • Save Sunnyztj/683b3da3d0e17b9664dc to your computer and use it in GitHub Desktop.
Save Sunnyztj/683b3da3d0e17b9664dc to your computer and use it in GitHub Desktop.
add tap to label and toggle url event
- (void)addTagGestureRecognizerToURLLabel {
// first image tag gesture recognizer setup
UITapGestureRecognizer *firstURLSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(url1TapEvent)];
firstURLSingleTap.numberOfTapsRequired = 1;
self.link1Url.userInteractionEnabled = YES;
[self.link1Url addGestureRecognizer:firstURLSingleTap];
UITapGestureRecognizer *secondURLSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(url2TapEvent)];
secondURLSingleTap.numberOfTapsRequired = 1;
self.link2Url.userInteractionEnabled = YES;
[self.link2Url addGestureRecognizer:secondURLSingleTap];
}
- (void)url1TapEvent {
[self toggleLinkToURL:self.link1Url.text];
}
- (void)url2TapEvent {
[self toggleLinkToURL:self.link2Url.text];
}
- (void)toggleLinkToURL:(NSString *)url {
NSString *urlString = nil;
if (url != nil && ![url isEqualToString:@""]) {
urlString = [urlString hasPrefix:@"http"] ? url : [NSString stringWithFormat:@"http://%@", url];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment