【发布时间】:2016-09-16 00:57:39
【问题描述】:
MacOS 10.12+、Xcode 8+、Swift 3:
我想以编程方式自定义 NSTableView 标头的字体和绘图。我知道对此有较早的问题,但我今天找不到任何有效的方法。
例如,我尝试继承 NSTableHeaderCell 来设置自定义字体:
class MyHeaderCell: NSTableHeaderCell {
override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
NSLog("MyHeaderCell is drawing")
font = NSFont.boldSystemFont(ofSize: 12)
super.drawInterior(withFrame: cellFrame, in: controlView)
}
}
然后在我的表格视图中使用该子类:
tableColumn.headerCell = MyHeaderCell()
我在控制台中看到消息“MyHeaderCell is drawing”,但表格标题的字体没有改变。
【问题讨论】:
-
您是否尝试设置
attributedStringValue? -
@Willeke 是的,我尝试设置属性字符串值。我的设置被忽略了。
-
@sam 在我使用
attributedStringValue实现的MyHeaderCell 中完美运行!所以我猜你的代码有问题。你可能会发现一些提示here -
@HeinrichGiesen, Willeke:当我在 drawInterior() 覆盖中分配属性字符串值时,它确实有效。我之前在 headerCell 上设置了属性字符串值属性,但该设置被忽略了。谢谢你们俩。
标签: macos cocoa nstableview swift3 nstableheadercell