Fix link ranges being stored incorrectly
This commit is contained in:
parent
65530dbfd0
commit
f28e73442b
|
@ -56,7 +56,8 @@ class StatusTableViewCell: UITableViewCell {
|
||||||
// print(status.content)
|
// print(status.content)
|
||||||
// print("---")
|
// print("---")
|
||||||
|
|
||||||
let text = attributedTextForNode(body)
|
let (text, links) = attributedTextForNode(body)
|
||||||
|
self.links = links
|
||||||
contentLabel.attributedText = text
|
contentLabel.attributedText = text
|
||||||
contentLabel.isUserInteractionEnabled = true
|
contentLabel.isUserInteractionEnabled = true
|
||||||
contentLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapOnContentLabel(_:))))
|
contentLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapOnContentLabel(_:))))
|
||||||
|
@ -74,14 +75,20 @@ class StatusTableViewCell: UITableViewCell {
|
||||||
textContainer.maximumNumberOfLines = contentLabel.numberOfLines
|
textContainer.maximumNumberOfLines = contentLabel.numberOfLines
|
||||||
}
|
}
|
||||||
|
|
||||||
func attributedTextForNode(_ node: Node) -> NSAttributedString {
|
func attributedTextForNode(_ node: Node) -> (NSAttributedString, [NSRange: URL]) {
|
||||||
switch node {
|
switch node {
|
||||||
case let node as TextNode:
|
case let node as TextNode:
|
||||||
return NSAttributedString(string: node.text())
|
return (NSAttributedString(string: node.text()), [:])
|
||||||
case let node as Element:
|
case let node as Element:
|
||||||
|
var links = [NSRange: URL]()
|
||||||
let attributed = NSMutableAttributedString()
|
let attributed = NSMutableAttributedString()
|
||||||
node.getChildNodes().forEach { child in
|
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() {
|
switch node.tagName() {
|
||||||
|
@ -102,7 +109,7 @@ class StatusTableViewCell: UITableViewCell {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return attributed
|
return (attributed, links)
|
||||||
default:
|
default:
|
||||||
fatalError("Unexpected node type: \(type(of: node))")
|
fatalError("Unexpected node type: \(type(of: node))")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue