Change selection color on view-based NSTableView

前端 未结 13 2075
别跟我提以往
别跟我提以往 2020-12-07 19:11

Standard highlighting color in OS X applications is blue.

Is it possible to change it to another color, e.g. gray?

Note that I am using the new view-based

13条回答
  •  伪装坚强ぢ
    2020-12-07 19:23

    I've mixed all methods described before and got code that exactly do what I want.

    • Selection not change color of textfields inside;
    • Rows remember selection and color of one;
    • Any strange outer borders and other leftovers appear.

      class AudioCellView: NSTableRowView {
      
          override func draw(_ dirtyRect: NSRect) {
              super.draw(dirtyRect)
              self.wantsLayer = true
              self.layer?.backgroundColor = NSColor.white.cgColor
          }
      
          override var isEmphasized: Bool {
              set {}
              get {
                  return false
              }
          }
      
          override var selectionHighlightStyle: NSTableView.SelectionHighlightStyle {
              set {}
              get {
                  return .regular
              }
          }
      
          override func drawSelection(in dirtyRect: NSRect) {
              if self.selectionHighlightStyle != .none {
                  let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5)
                  NSColor(calibratedWhite: 0.85, alpha: 0.6).setFill()
                  let selectionPath = NSBezierPath.init(rect: selectionRect)
                  selectionPath.fill()
              }
          }
      }
      

提交回复
热议问题