• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Vertically aligning text in a table while preserving fontSize bindings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vertically aligning text in a table while preserving fontSize bindings


  • Subject: Re: Vertically aligning text in a table while preserving fontSize bindings
  • From: Mike Abdullah <email@hidden>
  • Date: Thu, 22 Feb 2007 21:09:52 +0000

Pretty close, I suggest you override the various frame methods, it's a lot cleaner.

On 22 Feb 2007, at 18:29, George Orthwein wrote:

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];
}

Call [super drawWithFrame:inView:] instead to cover all possibilities. In fact to make things nicer, I like this approach:


- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
	NSSize drawingSize = [self cellSizeForBounds: cellFrame];
	float offset = (cellFrame.size.height - myCellSize.height) / 2;
	NSRect centeredFrame = NSInsetRect(cellFrame, 0.0, offset);

	[super drawWithFrame: centeredFrame inView: controlView];
}

To draw that correctly with editing enabled, you also need to offer a similar implementation in - editWithFrame:inView:editor:delegate:event: and - selectWithFrame:inView:editor:delegate:start:length:

I haven't experimented with it yet, but it may actually be simpler to override -cellSizeForBounds: and other similar methods.

Mike.


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:
40mikeabdullah.net


This email sent to email@hidden

_______________________________________________

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


  • Follow-Ups:
    • Re: Vertically aligning text in a table while preserving fontSize bindings
      • From: George Orthwein <email@hidden>
References: 
 >Vertically aligning text in a table while preserving fontSize bindings (From: George Orthwein <email@hidden>)

  • Prev by Date: Re: Core Data OpenGL
  • Next by Date: Access to value in dictionary from Interface Builder?
  • Previous by thread: Vertically aligning text in a table while preserving fontSize bindings
  • Next by thread: Re: Vertically aligning text in a table while preserving fontSize bindings
  • Index(es):
    • Date
    • Thread