Is a UILabel with a hyperlink inside a UITableViewCell supposed to open a web browser for safari?

I have a custom UITableViewCellwith two labels ( UILabel). Table cells are used to display information / text. Inside some of these cells (not all) there is text like this:

cell.myTextlabel.text = @"http://www.google.de"

Now, if I click on this text / link, the Safari web browser should open this web page. How can i do this?

Best regards Tim.

+3
source share
4 answers

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 we 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]];
    }
}
+9
source

UITextView UILabel, . dataDetectorTypes UIDataDetectorTypeLink.

+4

UITapGestureRecognizer, UIButtons .

UITapGestureRecognizer:

UILabel

UIButton:

UIButton Rounded Rect .

0

click Safari   [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @ "http://www.google.com" ]];

0

All Articles