【发布时间】:2018-11-26 02:48:51
【问题描述】:
我的目标是记录用户点击的所有链接。 链接嵌入在表格单元格内的 UITextView 中,当用户按下链接时,我希望它调用打印 url 的函数。
我看过类似的问题,例如 How to detect if a link, inside a UITextView has been clicked, Swift 和
How to intercept click on link in UITextView?
人们建议使用函数shouldInteractWithURL,但该函数从未被调用。
我想我可能将委托或函数设置在错误的位置,如果有人能告诉我应该如何设置,那就太好了。
以下是部分代码和页面截图:
class BSPdetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
func textView(textView: UITextView!, shouldInteractWithURL URL: NSURL!, inRange characterRange: NSRange) -> Bool {
print("Link Selected!")
return true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell", for: indexPath as IndexPath) as! detailCell
cell.label?.text = ""
cell.detailLinks.delegate = self
cell.detailLinks.isUserInteractionEnabled = true // default: true
cell.detailLinks.isEditable = false // default: true
cell.detailLinks.isSelectable = true // default: true
cell.detailLinks.dataDetectorTypes = [.link]
cell.detailTextLabel?.font = UIFont.fontAwesome(ofSize: 20)
cell.detailTextLabel?.text = ""
cell.detailTextLabel?.numberOfLines = 0;
cell.detailLinks?.text = "links are here"
var linkString = NSMutableAttributedString(string: "", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 22.0)])
let video = String.fontAwesomeIcon(code: "fa-video-camera")! + " "
let document = String.fontAwesomeIcon(code: "fa-file-text")! + "\n\n"
for i in 0..<stratList.count {
var stratURL = stratList[i].lowercased().replacingOccurrences(of: " ", with: "-")
var docLink = "http://" + stratURL
var videoLink = "http://" + stratURL
//setting the url
var attributedString = NSMutableAttributedString(string: video, attributes: [NSFontAttributeName: UIFont.fontAwesome(ofSize: 22)])
attributedString.addAttribute(NSLinkAttributeName, value: docLink, range: NSRange(location: 0, length: 1))
linkString.append(attributedString as NSAttributedString)
attributedString = NSMutableAttributedString(string: document, attributes: [NSFontAttributeName: UIFont.fontAwesome(ofSize: 22)])
attributedString.addAttribute(NSLinkAttributeName, value: videoLink, range: NSRange(location: 0, length: 1))
linkString.append(attributedString as NSAttributedString)
cell.label?.text = (cell.label?.text)! + "• " + stratList[i] + "\n\n"
cell.detailLinks?.attributedText = linkString
//tried tapgesture but it didn't work when I tapped on the links
//cell.detailLinks.addGestureRecognizer(UITapGestureRecognizer(target: cell.detailLinks, action: #selector(cell.detailLinks.singleTap(tap:))))
cell.backgroundColor = contentColors[rowNo]
cell.label?.textColor = UIColor.black
}
return cell
}
}
class detailCell: UITableViewCell
{
@IBOutlet weak var detailLinks: UITextView!
@IBOutlet weak var label: UILabel!
}
【问题讨论】:
-
UITextViewDelegate方法shouldInteractWithURL应该写在cellForRowAt外面而不是里面。 -
@GovindKumawat 谢谢!我试过了,这个函数仍然没有被调用。还有其他建议吗?
标签: swift uitableview url uitextview uitextviewdelegate