Re: Scaling images in NSButtonCell
Re: Scaling images in NSButtonCell
- Subject: Re: Scaling images in NSButtonCell
- From: Lane Schwartz <email@hidden>
- Date: Sat, 25 Aug 2001 16:37:55 -0500
- Organization: The Realm of Dowobeha
I was just wondering if this issue was ever resolved.
I am also trying to display an image (JPG) inside of an NSButton.
I want the NSImage to resize to fit into the NSButton,
but I can't figure out how to get the NSButton (or NSButtonView)
to tell me what size it is!
NSSize buttonSize = [[myImageButton cell] cellSize];
[img setSize:buttonSize];
The above doesn't seem to work. It puts the image in the button,
but the image is very tiny and in the center of the button.
Any help would be appreciated; my code as it stands is below.
Thanks,
Lane Schwartz
On Thursday, June 7, 2001, at 10:54 , Jens Baumeister wrote:
I have an NSMatrix with NSButtonCells. I create these cells dynamically at
runtime and assign them NSImages as labels.
The only problem I have is that I can't figure out a way to properly scale
the NSImage down to the NSButtonCell's size. I tried something like
[buttonImage setScalesWhenResized:YES];
[buttonImage setSize:[buttonMatrix cellSize]];
but it didn't work correctly. :(
This may be a bug in 10.0 with setting image sizes smaller. Unfortunately,
there isn't an easy way to work around it. You might try drawing the
image into another image that is smaller using -[NSImage drawInRect:]
Andrew
SimpleTest.h
------------
#import <Cocoa/Cocoa.h>
@interface SimpleTest : NSObject
{
IBOutlet id myImageButton;
}
- (IBAction)setFirst:(id)sender;
@end
SimpleTest.m
------------
#import "SimpleTest.h"
@implementation SimpleTest
- (IBAction)setFirst:(id)sender
{
int result;
NSOpenPanel *op = [NSOpenPanel openPanel];
NSMutableArray *fileTypes = [NSArray array];
[fileTypes addObject:@"jpg"];
[fileTypes addObject:@"JPG"];
[fileTypes addObject:@"jpeg"];
[fileTypes addObject:@"JPEG"];
[fileTypes addObject:@"'JPEG'"];
result = [op runModalForTypes:fileTypes];
if (result == NSOKButton) {
NSArray *filesToOpen = [op filenames];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:[filesToOpen objectAtIndex:0]];
NSSize buttonSize = [[myImageButton cell] cellSize];
[img setSize:buttonSize];
[myImageButton setImage:img];
[img release];
}
}
@end