views:

1509

answers:

4

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.

+2  A: 

What about:

CGSize window_size = my_window.frame.size;
fbrereto
+4  A: 

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.

Ken
ok here's what I have: -(NSSize)windowWillResize:(NSWindow*)sendertoSize:(NSSize)frameSize{ [length setStringValue:@"thisworks"]; [width setStringValue:@"test"]; return;}(the strings are tests to make sure it works)Everything is connected in IB correctly, it's all saved. But when I run the application it just shows "label" it doesn't say what I told it do.
Matt S.
Is the above code in a delegate? Can you update your question with the code you have to provide more context?
fbrereto
it's in the window delegate
Matt S.
matt: Did you remember to hook up those `length` and `width` outlets in IB? If they're not set, you're messaging `nil`, which does nothing.
Peter Hosey
yea, it's all hooked up right. I tried setting the text using awakeFromNib and it worked, but the -(NSSize) doesn't work. How exactly should I set them?
Matt S.
That code looks correct, with one exception: Notice that the return type of the method is not `void`. You must return a value. Specifically, you must return the size you want the window to resize to. Assuming you don't actually want any specific size or to constrain the size in any way, just return the size it proposed.
Peter Hosey
Make sure your object really is set as the window's delegate (which you would do in IB). Note that the application is not the window; that's a common mistake. Also, if you're targeting 10.6, you may need to declare your delegate class as conforming to the `NSWindowDelegate` protocol; I know you'll get a warning if you try to set it in code without that, but I don't know whether NSWindow tries to enforce that conformance if you build on 10.6.
Peter Hosey
ok, I have it set up now, however I don't think it's calling windowWillResize, I have an NSLog set up in there, and it isn't putting it in the console when I resize the window
Matt S.
Ok, I got it to work!
Matt S.
A: 

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.

Peter Hosey
how will that allow me to display the size using an NSTextField?
Matt S.
Oh, I thought you were asking to get the size and resize your views with it. Sorry. I endorse Ken's answer.
Peter Hosey
+1  A: 
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.

Ted Middleton
Alternatively, get the window's `frame`, then send the window a `contentRectForFrameRect:` message to convert it.
Peter Hosey