Thursday 16 April 2009

NSButton: setting an image in a button

I always have trouble to know how to set an image in a button. The following code snippet does the job:

NSString* im1 = [[NSBundle mainBundle] pathForResource:@"arrow right" ofType:@"png"];
NSURL* url2 = [NSURL fileURLWithPath:im1];
NSImage *image = [[NSImage alloc] initWithContentsOfURL: url2];


[toggleButton setImage: image] ;

NSString *im2 = [[NSBundle mainBundle] pathForResource:@"arrow left" ofType:@"png"];
url2 = [NSURL fileURLWithPath:im2];
image = [[NSImage alloc] initWithContentsOfURL: url2];
[toggleButton setAlternateImage: image] ;

The code above set's 2 images in a toggle button .

Note that if you want transparant images , you need .png files and not jpeg's (GIF also works)

No comments:

Post a Comment