2 yue1liang1chuan yue1liang1chuan 于 2013.04.10 11:01 提问

UILable链接打开safari浏览器

有自定义UITableViewCell 其中包含两枚UILabel。表格单元显示information/text。一些单元设置:

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

我想在点击这些text链接时,safari浏览器可以打开网页。应该怎么实现?

2个回答

xinyoulingxi1_tong
xinyoulingxi1_tong   2013.04.10 13:34
已采纳

设置userInteractionEnabled 为 YES 。添加一个姿势识别器:

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]];
    }
}
starwar2012
starwar2012   2013.04.10 15:15

如果你用UITextView代替UILabel,它会自动检测链接。

设置视图的dataDetectorTypesUIDataDetectorTypeLink

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!