在UITableViewCell内部带有超链接的UILabel应该打开safari webbrowser吗? [英] UILabel with a hyperlink inside a UITableViewCell should open safari webbrowser?

查看:131
本文介绍了在UITableViewCell内部带有超链接的UILabel应该打开safari webbrowser吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个标签的自定义 UITableViewCell UILabel )。表格单元格用于显示信息/文本。在其中一些单元格(不是全部)中,以这种方式设置文本:

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"

现在我想要点击此文字/链接, safari webbrowser应该打开这个网页。我怎么能这样做?

Now I want if I click this text / link, a safari webbrowser should open this webpage. How can I do this?

最好的问候蒂姆。

推荐答案

将userInteractionEnabled设置为标签的YES,并为其添加手势识别器:

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];

然后实施行动方法:

- (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]];
    }
}

这篇关于在UITableViewCell内部带有超链接的UILabel应该打开safari webbrowser吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆