I have an NSTableView with multiple text columns. By default, the dataCell for these columns is an instance of the Apple class NSTextFieldCell , which does all kinds of great things, but it draws text aligned with the top of the cell, and I want the text to be vertically centered in the cell.
NSTextFieldCell has an internal flag that you can use to center the text vertically, and it works great. However, since this is an internal flag, its use is not authorized by Apple, and it may simply disappear without warning in a future version. I am currently using this internal flag because it is simple and efficient. Apple obviously spent some time implementing this feature, so I don't like the idea of reimplementing it.
So my question is this: What is the correct way to implement what behaves exactly the same as Apple NStextFieldCell, but draws vertically centered text instead of aligned to the top?
For the record, here is my current "solution":
@interface NSTextFieldCell (MyCategories) - (void)setVerticalCentering:(BOOL)centerVertical; @end @implementation NSTextFieldCell (MyCategories) - (void)setVerticalCentering:(BOOL)centerVertical { @try { _cFlags.vCentered = centerVertical ? 1 : 0; } @catch(...) { NSLog(@"*** unable to set vertical centering"); } } @end
Used as follows:
[[myTableColumn dataCell] setVerticalCentering:YES];
vertical-alignment cocoa customization
e.James Aug 05 '09 at 19:25 2009-08-05 19:25
source share