-
-
Save laevandus/31ad37f4553c3d7ae6f8e399af458c25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override var attributedText: NSAttributedString? { | |
get { | |
return super.attributedText | |
} | |
set { | |
super.attributedText = { | |
guard let newValue = newValue else { return nil } | |
// Apply custom hyperlink attributes | |
let text = NSMutableAttributedString(attributedString: newValue) | |
text.enumerateAttribute(.hyperlink, in: NSRange(location: 0, length: text.length), options: .longestEffectiveRangeNotRequired) { (value, subrange, _) in | |
guard let value = value else { return } | |
assert(value is URL) | |
text.addAttributes(hyperlinkAttributes, range: subrange) | |
} | |
// Fill in font attributes when not set | |
text.enumerateAttribute(.font, in: NSRange(location: 0, length: text.length), options: .longestEffectiveRangeNotRequired) { (value, subrange, _) in | |
guard value == nil, let font = font else { return } | |
text.addAttribute(.font, value: font, range: subrange) | |
} | |
return text | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment