From f28e73442b0c3cd0490b48b9e3cafc661b415100 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 21 Aug 2018 21:43:43 -0400 Subject: [PATCH] Fix link ranges being stored incorrectly --- Tusker/Views/StatusTableViewCell.swift | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Tusker/Views/StatusTableViewCell.swift b/Tusker/Views/StatusTableViewCell.swift index c7cf09e8d2..5affd7c9d2 100644 --- a/Tusker/Views/StatusTableViewCell.swift +++ b/Tusker/Views/StatusTableViewCell.swift @@ -56,7 +56,8 @@ class StatusTableViewCell: UITableViewCell { // print(status.content) // print("---") - let text = attributedTextForNode(body) + let (text, links) = attributedTextForNode(body) + self.links = links contentLabel.attributedText = text contentLabel.isUserInteractionEnabled = true contentLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapOnContentLabel(_:)))) @@ -74,14 +75,20 @@ class StatusTableViewCell: UITableViewCell { textContainer.maximumNumberOfLines = contentLabel.numberOfLines } - func attributedTextForNode(_ node: Node) -> NSAttributedString { + func attributedTextForNode(_ node: Node) -> (NSAttributedString, [NSRange: URL]) { switch node { case let node as TextNode: - return NSAttributedString(string: node.text()) + return (NSAttributedString(string: node.text()), [:]) case let node as Element: + var links = [NSRange: URL]() let attributed = NSMutableAttributedString() node.getChildNodes().forEach { child in - attributed.append(attributedTextForNode(child)) + let (text, childLinks) = attributedTextForNode(child) + childLinks.forEach { range, url in + let newRange = NSRange(location: range.location + attributed.length, length: range.length) + links[newRange] = url + } + attributed.append(text) } switch node.tagName() { @@ -102,7 +109,7 @@ class StatusTableViewCell: UITableViewCell { break } - return attributed + return (attributed, links) default: fatalError("Unexpected node type: \(type(of: node))") }