How to change font size of NSTableHeaderCell
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.
Customise NSTableView Header, background color, remove border
First you would assign your a custom NSTableHeaderCell to each cell of NSTableView.
This can be done in a subclass of NSTableView (as below) or in the View controller (viewDidLoad)
override func awakeFromNib() {
for column in self.tableColumns{
column.headerCell = HeaderCell(textCell: column.headerCell.stringValue)
}
}
In your custom NSTableHeaderCell you can override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView)
to customise the drawing and text.
override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
NSColor.green.set()
let rect = NSRect(x: cellFrame.origin.x, y: cellFrame.origin.y - 3, width: cellFrame.size.width - 2, height: cellFrame.size.height + 10)
NSBezierPath(rect: rect).fill()
let str = NSAttributedString(string: stringValue, attributes:
[NSAttributedString.Key.foregroundColor: NSColor.red,
NSAttributedString.Key.font: NSFont(name: "Skia", size: 14)])
str.draw(in: cellFrame)
}
To customise further cell drawing (like borders) you can override func draw(withFrame cellFrame: NSRect, in controlView: NSView)
as well.
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
self.drawInterior(withFrame: cellFrame, in: controlView)
}
Of course you can use hardcoded attributes or the ones provided by the cell.
How to increase the height of NSTableHeaderView?
Following link helped me in solving the issue.
http://lists.apple.com/archives/cocoa-dev/2003/Feb/msg00676.html
You need to set the Frame for NSClipView
, NSTableHeaderView
and the CornerView
This is how I implemented the same in Code.
for(NSView * subview in [topScrollView subviews])
{
for(NSView * subSubView in [subview subviews])
{
if([[subSubView className] isEqualToString:@"NSTableHeaderView"] && [[subview className] isEqualToString:@"NSClipView"])
{
[subSubView setFrameSize:NSMakeSize(subSubView.frame.size.width, subSubView.frame.size.height+5)];//HeaderView Frame
[subview setFrameSize:NSMakeSize(subview.frame.size.width, subview.frame.size.height+5)];//ClipView Frame
}
}
if ([[subview className] isEqualToString:@"_NSCornerView"])
{
[subview setFrameSize:NSMakeSize(subview.frame.size.width, subview.frame.size.height+5)]; //CornerView Frame
}
}
How do I override layout of NSTableHeaderView?
Actually both the answers from mprudhom and Pronto contributed nicely to the final fix. Many thanks to both of you.
I thought I'd post my final answer so anyone following on can see how I fixed it.
I couldn't use mprudhom's code alone as my table only had a NSTableHeaderView and no TableHeaderCells. Fortunately Pronto came to the rescue on that:
class MyTable: NSTableView, NSTableViewDataSource, NSTableViewDelegate
{
override func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
tableColumn!.headerCell = MyTableHeaderCell(textCell: tableColumn!.identifier)
}
}
I used the NSTableHeaderCell code directly from mprudhom:
final class MyTableHeaderCell : NSTableHeaderCell
{
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
override init(textCell: String)
{
super.init(textCell: textCell)
self.font = NSFont.boldSystemFontOfSize(14)
}
override func drawWithFrame(cellFrame: NSRect, inView controlView: NSView)
{
//super.drawWithFrame(cellFrame, inView: controlView)//, since that is what draws borders
self.drawInteriorWithFrame(cellFrame, inView: controlView)
}
override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView)
{
let titleRect = self.titleRectForBounds(cellFrame)
self.attributedStringValue.drawInRect(titleRect)
}
}
Once these two methods were implemented, I had one last problem in that the NSTableHeaderView was drawn without the border as required but did have a grey background. So I overrode the NSTableHeaderView class with this method:
class ForecastHeaderView : NSTableHeaderView
{
required init?(coder: NSCoder)
{
super.init(coder: coder)
}
override init(frame frameRect: NSRect)
{
super.init(frame: frameRect)
self.wantsLayer = true
}
override func drawLayer(layer: CALayer, inContext ctx: CGContext)
{
super.drawLayer(layer, inContext: ctx)
layer.backgroundColor = CGColorCreateGenericGray(1.0, 1.0)
}
}
Related Topics
Mapping Swift Combine Future to Another Future
How to Create Generic Convenience Initializer in Swift
Can't Load Images on MAC Screensaver Release Build (It Works on Xcode Debug Build)
How to Check If a Variable Is Nil
Why Would One Use Nested Classes
Iocreateplugininterfaceforservice Returns Mysterious Error
How to Draw a Line Between Two Points Over an Image in Swift 3
Shorthand for Wrapping a Swift Variable in an Optional
Passing in Variable Number of Args from One Function to Another in Swift
Issue with Returning a Directory Enumerator from Nsfilemanager Using Enumeratoraturl in Swift
How to Write to a Variable from Within The Firebase Getdocument Function (Swift)
Converting Scanlocation from Utf16 Units to Character Index in Nsscanner (Swift)
Vapor 3 - How to Check for Similar Email Before Saving Object
How to a Convert a Dictionary Slice to a Dictionary in Swift
How to Disable The Automatic Activation of an Arcoachingoverlayview