ios:如何在uilabel中设置url链接?

时间:2014-02-20 07:20:13

标签: ios objective-c json uitableview

嗨朋友我的项目是基于从url解析json数据,我解析了数据并在屏幕上显示但我的问题是,我从json收到的一些数据来自链接。 but when i viewd that in label it looks like normal data, not as linki need to view it as link。当用户点击链接时,它会在移动浏览器中转到相应的网页。请帮我说明怎么做。这是我试过的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SuccessCell";
    SuccessListCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
    cell.title.text = (NSString *)[array1 objectAtIndex:indexPath.row];
    cell.linklabel.text = (NSString *)[array3 objectAtIndex:indexPath.row];//Here i need show this data as link
    cell.datetime.text = @"Success Story URL(Pictures,Videos,etc.);";
    cell.desc.text = (NSString *)[array11 objectAtIndex:indexPath.row];
    return cell;
}

请先帮助我如何操作

3 个答案:

答案 0 :(得分:3)

使用 UITextView 代替 UILabel 如果点击它,它将调用委托方法,请检查以下代码:

    UITextView *textView = [[UITextView alloc] init];
    textView.frame = CGRectMake(10,200, 300, 30);
    textView.scrollEnabled = NO;
    textView.text=@" This is testing url here HTTP://WWW.GOOGLE.COM";
    textView.editable = NO;
    textView.dataDetectorTypes = UIDataDetectorTypeLink;

    textView.delegate = self;
    [self.view addSubview:textView];

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
{
    return YES;
}

注意:它仅支持> = IOS 7

它可以帮到你。

快乐的编码......:)

答案 1 :(得分:0)

尝试控件FancyLabel。这正是你所需要的。

如果你遇到困难,你可以试试OHAttributedLabel - 这样会更简单。

答案 2 :(得分:0)

使用UIWebView并设置它的dataDetectorTypes属性或使用按钮,点击活动尝试使用以下代码,

-(IBAction)openLink:(UIButton *)sender
{
  NSString*myurl=sender.titleLabel.text;
  NSURL *url = [NSURL URLWithString:myurl];
  [[UIApplication sharedApplication] openURL:url];
}
相关问题