Vertically aligning text in a table while preserving fontSize bindings
Vertically aligning text in a table while preserving fontSize bindings
- Subject: Vertically aligning text in a table while preserving fontSize bindings
- From: George Orthwein <email@hidden>
- Date: Thu, 22 Feb 2007 13:29:09 -0500
I have a table with NSTextFieldCells as well as NSButtonCells and
NSPopUpButtonCells. Of course, the text fields do not vertically
center like the buttons and popups.
Vertically aligning text has come up several times in the archives,
but I didn't see any definitive solutions. Some suggest using
attributedStrings and others suggested subclassing and overriding
drawWithFrame:.
I'm not sure where to even start with attributedStrings. My text
cells are getting fed NSStrings from Core Data via bindings, so
perhaps I cannot send attributedStrings easily in this case? I tried
using setAttributedStringValue in tableView:willDisplayCell:... but
the baseline offset isn't working:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell
forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSDictionary *attr = [NSDictionary dictionaryWithObject:[NSNumber
numberWithFloat:-10.0] forKey:NSBaselineOffsetAttributeName];
[aCell setAttributedStringValue:[[[NSAttributedString alloc]
initWithString:[aCell stringValue] attributes:attr] autorelease]];
}
I had some success with drawWithFrame:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSSize myCellSize = [self cellSizeForBounds:cellFrame];
if (cellFrame.size.height > myCellSize.height) {
float offset = (cellFrame.size.height - myCellSize.height)/2;
cellFrame.origin.y += offset;
cellFrame.size.height -=offset;
}
[self drawInteriorWithFrame:cellFrame inView:controlView];
}
That works somewhat. The main problem is when you go to edit the
text, it jumps to the top of the cell. Do I need to get into field
editor stuff? I've never done anything with that, so I'd like to know
if I'm on the right track.
I briefly played with putting my code in drawInteriorWithFrame:
instead, but then I have to draw everything manually and be aware of
my fontSize binding.
Am I on the right track at all?
Thanks!
George
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden