UILabel with a hyperlink inside a UITableViewCell should open safari webbrowser?
I have a custom UITableViewCell
with two labels (UILabel
). The table cells are used to display information / text. Inside some of these cells (not all) there is text in this way set:
cell.myTextlabel.text = @"http://www.google.de"
开发者_运维知识库Now I want if I click this text / link, a safari webbrowser should open this webpage. How can I do this?
Best Regards Tim.
Set userInteractionEnabled to YES of your label and add a gesture recognizer to it:
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:gestureRec];
[gestureRec release];
Then implement the action method:
- (void)openUrl:(id)sender
{
UIGestureRecognizer *rec = (UIGestureRecognizer *)sender;
id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches];
if ([hitLabel isKindOfClass:[UILabel class]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:((UILabel *)hitLabel).text]];
}
}
If you use a UITextView
instead of a UILabel
it will automatically handle link detection. Set the view's dataDetectorTypes
to UIDataDetectorTypeLink
.
Inside your click event you can open safari browser by the following code [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
精彩评论