Моя цель - регистрировать все ссылки, по которым нажимает пользователь. Ссылки встроены в UITextView внутри ячейки таблицы, и когда пользователь нажимает ссылку, я хочу, чтобы она вызывала функцию, которая печатает URL-адрес.
Я просмотрел похожие вопросы, такие как Как определить, была ли нажата ссылка внутри UITextView, Swift и Как перехватить щелчок по ссылке в 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
, а не внутри. - person Govind Kumawat   schedule 16.06.2018