Re: How do I draw text on a custom button?
Re: How do I draw text on a custom button?
- Subject: Re: How do I draw text on a custom button?
- From: "John C. Randolph" <email@hidden>
- Date: Sat, 21 Jun 2008 03:00:13 -0700
Michael,
By implementing -drawRect: in your view class, you become responsible
for all drawing in that view. The code below doesn't attempt to draw
the text, it only sets a value.
To draw text in the current drawing context, see the -
drawAtPoint:withAttributes: method of NSString for one way to to do so.
I won't go into the intricacies of NSControl and NSCell just yet.
-jcr
On Jun 21, 2008, at 12:44 AM, Michael A. Crawford wrote:
I'm using a CustomView that inherits from NSButton. I have no
problem drawing the graphical representation of the button in the
view but it is not immediately obvious to me how to draw text.
setTitle does not work with my custom button. Can you point me to
some examples? Here is the code:
#import <Cocoa/Cocoa.h>
@interface TwoStateButton : NSButton
{}
@end
- (void)drawRect:(NSRect)rect
{
// draw initial black button
NSRect bounds = [self bounds];
[[NSColor blackColor] set];
[NSBezierPath fillRect:bounds];
// draw green LED inset in button (grey if not on)
bounds.origin.x += (bounds.size.width * 0.25) / 2;
bounds.origin.y += bounds.size.height * 0.25;
bounds.size.width *= 0.75;
bounds.size.height *= 0.25;
if ( NSOnState == [self state] )
{
[[NSColor greenColor] set];
}
else
{
[[NSColor lightGrayColor] set];
}
[NSBezierPath fillRect:bounds];
[self setTitle:@"NDB"];
}
_______________________________________________
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
_______________________________________________
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