Fix link ranges being stored incorrectly
This commit is contained in:
parent
5bd7f13125
commit
9443aaf8a6
|
@ -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))")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue