Created
July 6, 2020 19:06
-
-
Save pilky/846868844e10851eafba04925927fd5f to your computer and use it in GitHub Desktop.
A source list cell that correct colours the label text of a drag image when selected
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
class SourceListTableCellView: NSTableCellView { | |
override var draggingImageComponents: [NSDraggingImageComponent] { | |
let components = super.draggingImageComponents | |
guard | |
self.backgroundStyle == .emphasized, //emphasized = selected | |
let textField = self.textField, | |
let newStyle = textField.attributedStringValue.mutableCopy() as? NSMutableAttributedString, | |
let labelIndex = components.firstIndex(where: { $0.key == .label }) | |
else { | |
return components | |
} | |
//Set to the label colour and draw | |
newStyle.addAttribute(.foregroundColor, value: NSColor.labelColor, range: NSMakeRange(0, newStyle.length)) | |
let image = NSImage(size: textField.bounds.size, flipped: false) { (rect) -> Bool in | |
newStyle.draw(in: rect) | |
return true | |
} | |
//We'll just update the label contents as everything else is already setup for us | |
components[labelIndex].contents = image | |
return components | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment