Fix links in profile field values not at the beginning of the string not being tappable
Fixes #501
This commit is contained in:
parent
9990d50e3e
commit
20692b0630
|
@ -39,24 +39,24 @@ class ProfileFieldValueView: UIView {
|
||||||
|
|
||||||
let converted = NSMutableAttributedString(attributedString: ProfileFieldValueView.converter.convert(field.value))
|
let converted = NSMutableAttributedString(attributedString: ProfileFieldValueView.converter.convert(field.value))
|
||||||
|
|
||||||
var range = NSRange(location: 0, length: 0)
|
converted.enumerateAttribute(.link, in: converted.fullRange) { value, range, stop in
|
||||||
if converted.length != 0,
|
guard value != nil else { return }
|
||||||
let url = converted.attribute(.link, at: 0, longestEffectiveRange: &range, in: converted.fullRange) as? URL {
|
if self.link == nil {
|
||||||
link = (converted.attributedSubstring(from: range).string, url)
|
self.link = (converted.attributedSubstring(from: range).string, value as! URL)
|
||||||
|
}
|
||||||
|
#if os(visionOS)
|
||||||
|
converted.addAttribute(.foregroundColor, value: UIColor.link, range: range)
|
||||||
|
#else
|
||||||
|
converted.addAttribute(.foregroundColor, value: UIColor.tintColor, range: range)
|
||||||
|
#endif
|
||||||
|
// the .link attribute in a UILabel always makes the color blue >.>
|
||||||
|
converted.removeAttribute(.link, range: range)
|
||||||
|
}
|
||||||
|
|
||||||
|
if link != nil {
|
||||||
label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(linkTapped)))
|
label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(linkTapped)))
|
||||||
label.addInteraction(UIContextMenuInteraction(delegate: self))
|
label.addInteraction(UIContextMenuInteraction(delegate: self))
|
||||||
label.isUserInteractionEnabled = true
|
label.isUserInteractionEnabled = true
|
||||||
|
|
||||||
converted.enumerateAttribute(.link, in: converted.fullRange) { value, range, stop in
|
|
||||||
guard value != nil else { return }
|
|
||||||
#if os(visionOS)
|
|
||||||
converted.addAttribute(.foregroundColor, value: UIColor.link, range: range)
|
|
||||||
#else
|
|
||||||
converted.addAttribute(.foregroundColor, value: UIColor.tintColor, range: range)
|
|
||||||
#endif
|
|
||||||
// the .link attribute in a UILabel always makes the color blue >.>
|
|
||||||
converted.removeAttribute(.link, range: range)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
label.numberOfLines = 0
|
label.numberOfLines = 0
|
||||||
|
|
Loading…
Reference in New Issue