Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the width and height of NSView and NSImageView

I have a NSView, there is a NSImageView on the NSView

in the source codes of the NSView

I wrote:

NSImage *image = [[[NSImage alloc] initWithContentsOfURL:url] autorelease];
NSImageView *newImageView = nil;
newImageView = [[NSImageView alloc] initWithFrame:[self bounds]];
[newImageView setImage:image];
[newImageView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

Is there a way to read the width and height of the NSView and NSImageView?

like image 327
monsabre Avatar asked Jan 19 '23 10:01

monsabre


2 Answers

The size of any view (NSView, NSImageView, etc.) is view.frame.size or view.bounds.size. In both cases it is an identical NSSize struct. You can write code like this:

NSSize size = newImageView.frame.size;
NSLog(@"size: %@", NSStringFromSize(size));
NSLog(@"size: %f x %f", size.width, size.height);

To change it, you need to update the view frame property:

NSRect frame = view.frame;
frame.size = NSMakeSize(<#new width#>, <#new height#>);
view.frame = frame;
like image 132
djromero Avatar answered Feb 20 '23 05:02

djromero


Try the - (NSRect)bounds method.

like image 36
Michael Price Avatar answered Feb 20 '23 05:02

Michael Price