Skip to content

Instantly share code, notes, and snippets.

@pilky
Created July 6, 2020 19:06
Show Gist options
  • Save pilky/846868844e10851eafba04925927fd5f to your computer and use it in GitHub Desktop.
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
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