如何在Swift中的UILabel上的html链接中获取代码

时间:2015-11-23 02:04:33

标签: html swift hyperlink uilabel

我必须在TableViewCell中的textLabel上创建几个html链接。而如果  用户触摸链接,我必须获取链接中的代码并将其发送给其他人 功能

我在TableViewCell中的textLabel上做了几个链接,如下所示:

let myText = "The Code of this Doc: <a href=code:1234>1234</a>. The Code
of that Doc : <a href=code:4567>4567</a>. If you touch code number, you can see more information."

let attrString = try! NSMutableAttributedString(data:
myText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion:
true)!, options: [ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType], documentAttributes: nil)

textLabel.attributedText = attrString

但我不知道如何在用户触及的链接中获取代码。 请帮帮我。

感谢。

1 个答案:

答案 0 :(得分:0)

我对你问的具体细节有点不清楚。每个表格单元格都有不同的代码吗?如果是这样的话:

创建文本时将文本存储在数组中。让细胞使用以下方法获取文本:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   let code = nameOfArray[indexPath.row]
}

如果你想在同一个单元格中使用不同的代码,那么我建议使用单元格内的按钮,并将按钮的标题设置为你想要的任何代码。