Re: Custom cell wanted
Re: Custom cell wanted
- Subject: Re: Custom cell wanted
- From: Keith Blount <email@hidden>
- Date: Wed, 10 Mar 2010 10:05:06 -0800 (PST)
Apologies all for the mis-titled e-mail. Resending for the sake of the archives.
---
This is the relevant part of my cell subclass:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if(hasCount)
{
NSString * number = [NSString stringWithFormat:@"%i", count];
// Use the current font point size as a guide for the count font size
CGFloat pointSize = [[self font] pointSize];
// Create attributes for drawing the count.
NSDictionary* attributes = [[NSDictionaryalloc] initWithObjectsAndKeys:[NSFontnonNilFontWithName:@"Helvetica-Bold"size:pointSize], NSFontAttributeName,
(countTextColor!= nil? countTextColor: [NSColorwhiteColor]), NSForegroundColorAttributeName,
nil];
NSSize numSize = [number sizeWithAttributes:attributes];
// Compute the dimensions of the count rectangle.
NSInteger cellWidth = MAX(numSize.width + 6, 20.0);
NSRect countFrame;
CGFloat rightPadding = 5.0;
NSDivideRect(cellFrame, &countFrame, &cellFrame, cellWidth+rightPadding, NSMaxXEdge);
countFrame.size.width -= rightPadding;
if([selfdrawsBackground])
{
[[selfbackgroundColor] set];
NSRectFill(cellFrame);
}
countFrame.origin.y += 1;
countFrame.size.height -= 2;
CGFloat radius = numSize.height / 2;
NSBezierPath * roundedRect = [NSBezierPath bezierPathWithRoundedRect:countFrame cornerRadius:radius];
[(countBackgroundColor!= nil? countBackgroundColor: [NSColorgrayColor]) set];
[roundedRect fill];
// Draw the count in the rounded rectangle we just created.
//NSPoint point = NSMakePoint(NSMidX(countFrame) - numSize.width / 2.0f, NSMidY(countFrame) - numSize.height / 2.0f );
NSPoint point = NSMakePoint(countFrame.origin.x + (countFrame.size.width-numSize.width)/2.0,
countFrame.origin.y + (countFrame.size.height-numSize.height)/2.0);
[number drawAtPoint:point withAttributes:attributes];
[attributes release];
}
[superdrawInteriorWithFrame:cellFrame inView:controlView];
}
Note the NSBezierPath method is from Andreas Meyer's category because I support 10.4, so could be replaced with the equivalent 10.5 AppKit method. The accessors it uses (hasCount, count etc) should all be fairly straightforward, and nonNilFontForFont: just returns the system or user font if the font passed in is nil. (I would have included the whole class but it's long and does a lot more than you need, including handling centring vertically, drawing icons and labels and so on.)
I set the countBackgroundColor in -willDisplayCell depending on whether the outline has the focus or not. I set it to [NSColor colorWithCalibratedWhite:0.604 alpha:1.0] if the outline view doesn't have the focus or [NSColor colorWithCalibratedRed:0.51 green:0.592 blue:0.741 alpha:1.0] if it does (which closely matches the colour in Mail).
All the best,
Keith
---------
Message: 13
Date: Thu, 11 Mar 2010 01:35:34 +1100
From: Graham Cox <email@hidden>
Subject: Custom cell wanted
To: Cocoa-Dev List <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=us-ascii
Hi,
I need a custom cell for a table/outline view that is exactly like Mail's 'unread' count - the grey lozenge with a number in it. It's probably only a few hours work but maybe someone has already done it and would be willing to share?
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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