6
votes

Is there any way I get get the size of an NSWindow (in pixels) and display it? So when the person resizes the window the text will change and display the new size.

4

4 Answers

12
votes

If you implement the method

- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize

on an object you set as the delegate of the window, it will be called whenever the window changes size. From there you can update the text field you use for displaying the size.

6
votes

What about:

CGSize window_size = my_window.frame.size;
3
votes
NSSize myNSWindowSize = [ [ myNSWindow contentView ] frame ].size;

...should be what you're looking for. fbrereto's suggestion is what you should use if you want the size including the NSWindow's title bar.

0
votes

Instead of attempting to handle resizes, you should instead make sure your views' autoresizing masks are set correctly. In Interface Builder, you do this on the Size inspector (⌘3) with the views selected.

If you've lain out an entire view hierarchy without setting their autoresizing masks, you have a bit of tedium ahead of you, but it's not hard (just tedious), and won't take very long. This is why you should do set views' autoresizing masks as you create them.

Once the autoresizing masks are set, the views will resize themselves automatically; unless your interface is very complicated, or any views can go down to zero size, you won't need to intervene.