How to change font size of NSTableHeaderCell

China☆狼群 提交于 2020-01-03 21:08:11

问题


I am trying to change the font size of my NSTableView within my code to allow the user to change it to their liking. I was successful by changing the font size of each NSTableCellView but failed to do so by the header cells.

I was trying to do it like this

let headerCell = NSTableHeaderCell()
let font = NSFont(name: "Arial", size: 22.0)
headerCell.stringValue = "firstname"
headerCell.font = font
customerTable.tableColumns[0].headerCell = headerCell

The stringValue of the header cell will be set accordingly but the size does not change. How can I change the font size of my headers?

Thanks

Oliver


回答1:


You can create a NSTableHeaderCell subclass and implement the property you want to change.

In Objective-C (I'm not good at Swift):

@implementation CustomTableHeaderCell

-(NSFont *)font {
    return [NSFont fontWithName:@"Arial" size:22];
}

// you can alse custom textColor
-(NSColor *)textColor {
    return [NSColor redColor];
}

@end

Assign CustomTableHeaderCell:

CustomTableHeaderCell *headerCell = [[CustomTableHeaderCell alloc] init];
headerCell.stringValue = @"Header title";
self.tableView.tableColumns[0].headerCell = headerCell;

In Cocoa, there are many things you can't change its style by cell.font = ..., you need to create a subcalss.




回答2:


So, finally I was only able to solve this with subclassing NSTableHeaderCell. It was somehow strange as Swift and Cocoa always tend to favor composition over inheritance but anyway.

Swift 3.1

final class CustomTableHeaderCell : NSTableHeaderCell {

    override init(textCell: String) {
        super.init(textCell: textCell)
        self.font = NSFont.boldSystemFont(ofSize: 18) // Or set NSFont to your choice
        self.backgroundColor = NSColor.white
    }

    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        // skip super.drawWithFrame(), since that is what draws borders
        self.drawInterior(withFrame: cellFrame, in: controlView)
    }

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        let titleRect = self.titleRect(forBounds: cellFrame)
        self.attributedStringValue.draw(in: titleRect)
    }
}



回答3:


Changing attributedStringValue will do the trick

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.tableColumns.forEach { (column) in

        column.headerCell.attributedStringValue = NSAttributedString(string: column.title, attributes: [NSFontAttributeName: NSFont.boldSystemFont(ofSize: 18)])

        // Optional: you can change title color also jsut by adding NSForegroundColorAttributeName
    }
}

https://i.stack.imgur.com/IXJyu.png



来源:https://stackoverflow.com/questions/43510646/how-to-change-font-size-of-nstableheadercell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!