swift 如何更改NSTableHeaderView的背景色而不隐藏列标题?

kmynzznz  于 12个月前  发布在  Swift
关注(0)|答案(3)|浏览(82)

我在我的Swift OS X应用程序上使用了基于视图的NSTableView,它有12列。
我子类我的headerview,并试图改变不同的东西的背景颜色,但他们都隐藏我的标题也。
这是许多尝试做的例子之一:

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)

    // Drawing code here.
    var myLayer = CALayer()
    myLayer.frame = self.frame
    myLayer.backgroundColor = NSColor.greenColor().CGColor
    self.layer?.addSublayer(myLayer)

字符串
这个例子上填写整个headerview与绿色,所以我试图告诉我,我想标题的我列这样:

self.tableView?.tableColumnWithIdentifier("date")?.title = "Date"


......但什么都没发生。有人能帮帮忙吗?

webghufk

webghufk1#

在我的一个应用程序中,我想做(几乎)相同的事情,再加上一点(改变headerView的高度)。因此,我子类化**(不是NSTableHeaderView,而是)**NSTableHeaderCell与我自己的子类复制标题,字体,stringValue等。从原始单元格。(我无法在IB中做到这一点。)
我首先用我的类的对象替换了headerView的所有单元格。在这里(在方法- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView中)我设置了backGroundColor。这工作得很好。
我的提示:不要在(NSTableHeaderView的子类)中设置backGroundColor,而是在NSTableHeaderCell的子类中设置。

vltsax25

vltsax252#

class ViewController: NSViewController {

@IBOutlet weak var listTableView: NSTableView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Set custom header cell class for each table column
    for aColumn in listTableView.tableColumns {
        let customHeaderCell = CustomHeaderCell(textCell: aColumn.headerCell.stringValue)
        customHeaderCell.font = NSFont.systemFont(ofSize: 13, weight: .regular)
        customHeaderCell.textColor = NSColor.white
        aColumn.headerCell = customHeaderCell
    }
  }
}


class CustomHeaderCell: NSTableHeaderCell {

// Override the initializer to set up your custom properties
override init(textCell: String) {
    super.init(textCell: textCell)
    commonInit()
}

required init(coder: NSCoder) {
    super.init(coder: coder)
    commonInit()
}

// Common initialization code
private func commonInit() {
    // Customize any additional properties or settings here
    self.drawsBackground = true
    self.backgroundColor = NSColor.red
}

// Override the drawing method to customize the appearance
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
    // Draw background color
    self.backgroundColor?.set()
    NSBezierPath(rect: cellFrame).fill()

    // Draw separator line
    NSColor.black.withAlphaComponent(0.2).set()
    NSBezierPath(rect: NSRect(x: cellFrame.origin.x, y: cellFrame.origin.y, width: cellFrame.size.width, height: 1)).fill()

    // Draw header text
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    let headerCellText = NSAttributedString(string: stringValue, attributes:
                                            [NSAttributedString.Key.foregroundColor: NSColor.black,
                                             NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13, weight: .regular),
                                             NSAttributedString.Key.paragraphStyle: paragraphStyle,
                                             NSAttributedString.Key.baselineOffset: -6.0
                                            ])
    headerCellText.draw(in: cellFrame)
}
}

字符串
参考号:https://developer.apple.com/forums/thread/735989

xmq68pz9

xmq68pz93#

添加背景和分隔符颜色

class ViewController: NSViewController {

@IBOutlet weak var listTableView: NSTableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Set custom header cell class for each table column
    for aColumn in listTableView.tableColumns {
        let customHeaderCell = CustomHeaderCell(textCell: aColumn.headerCell.stringValue)
        customHeaderCell.font = NSFont.systemFont(ofSize: 13, weight: .regular)
        customHeaderCell.textColor = NSColor.white
        aColumn.headerCell = customHeaderCell
    }
  }
}

class CustomHeaderCell: NSTableHeaderCell {

override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {

    // Draw background color
    NSColor.red.setFill()
    NSBezierPath(rect: cellFrame).fill()

    // Draw separator line
    NSColor.gray.set()
    NSBezierPath(rect: NSRect(x: cellFrame.origin.x + cellFrame.size.width - 1.0, y: cellFrame.origin.y, width: 1.0, height: cellFrame.size.height)).fill()

    // Draw header text
    let interiorFrame = CGRect(x: cellFrame.origin.x, y: cellFrame.origin.y + 4.0, width: cellFrame.size.width, height: 14.0)
    drawInterior(withFrame: interiorFrame, in: controlView)
 }
}

字符串

相关问题