Swift 如何检测是否单击了选项卡单元格中UITextView中的链接
我的目标是记录用户单击的所有链接。 链接嵌入在表单元格内的UITextView中,当用户按下链接时,我希望它调用打印url的函数 我看过类似的问题,比如和 如果有人建议使用函数Swift 如何检测是否单击了选项卡单元格中UITextView中的链接,swift,uitableview,url,uitextview,uitextviewdelegate,Swift,Uitableview,Url,Uitextview,Uitextviewdelegate,我的目标是记录用户单击的所有链接。 链接嵌入在表单元格内的UITextView中,当用户按下链接时,我希望它调用打印url的函数 我看过类似的问题,比如和 如果有人建议使用函数,则应与URL交互,但该函数从未被调用 我想我可能将委托或函数设置在了错误的位置,如果有人能告诉我应该如何设置,那就太好了 以下是该页面的缩写代码和一些屏幕截图: class BSPdetailViewController: UIViewController, UITableViewDelegate, UITableVie
,则应与URL
交互,但该函数从未被调用
我想我可能将委托或函数设置在了错误的位置,如果有人能告诉我应该如何设置,那就太好了
以下是该页面的缩写代码和一些屏幕截图:
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!
}
类BSPDailViewController:UIViewController、UITableViewDelegate、UITableViewDataSource、UITextViewDelegate{
func textView(textView:UITextView!,应与URL:NSURL!进行交互,在范围字符范围内:NSRange)->Bool{
打印(“已选择链接!”)
返回真值
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“detailCell”,for:indexPath作为indexPath)作为!detailCell
cell.label?.text=“”
cell.detailLinks.delegate=self
cell.detailLinks.isUserInteractionEnabled=true//默认值:true
cell.detailLinks.isEditable=false//默认值:true
cell.detailLinks.isSelectable=true//默认值:true
cell.detailLinks.dataDetectorTypes=[.link]
cell.detailTextLabel?.font=UIFont.fontAwesome(大小:20)
cell.detailTextLabel?.text=“”
cell.detailTextLabel?.numberOfLines=0;
cell.detailLinks?.text=“链接在这里”
var linkString=nsmutableAttributeString(字符串:“”,属性:[NSFontAttributeName:UIFont.systemFont(ofSize:22.0)])
let video=String.fontAwesomeIcon(代码:“fa摄像机”)!+“”
let document=String.fontAwesomeIcon(代码:“fa文件文本”)!+“\n\n”
对于0中的i..UITextViewDelegate
方法应与URL进行交互
应在cellForRowAt
外部写入,而不是内部写入。您的标准UITextView设置应如下所示,不要忘记委托和数据检测类型
cell.detailLinks.delegate = self
detailLinks.isUserInteractionEnabled = true // default: true
detailLinks.isEditable = false // default: true
detailLinks.isSelectable = true // default: true
detailLinks.dataDetectorTypes = [.link]
UITextViewDelegate
方法应与URL进行交互
:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("Link Selected!")
return true
}
UITextViewDelegate
方法应该与URL交互
应该在cellForRowAt
外部编写,而不是内部编写。@GovindKumawat谢谢!我已经尝试过了,函数仍然没有被调用。还有其他建议吗?再次感谢!我已经尝试过了这些,但函数仍然没有被调用。我已经更新了代码以反映您的建议。@haedaldal这里的问题是您的UITextViewDelegate
方法shoulInteractWithURL
您使用的方法不正确,缺少一个参数,可能是在以前的swift版本中使用的。这真的救了我一命。我显然是从某个这里有其他的,但无法让它工作。终于让它工作了!