tags:

views:

139

answers:

2

For my application I want to set a custom length and width for my NSWindow from within the application itself. I have everything set up (the NSTextfields) however I'm stumped with how I should do it.

+3  A: 

You want the setFrame:display: or setFrame:display:animate: method on NSWindow.

From the documentation:

- (void)setFrame:(NSRect)windowFrame display:(BOOL)displayViews

Parameters
windowFrame
The frame rectangle for the window.

displayViews
Specifies whether the window redraws the views that need to be displayed. When YES the window sends a displayIfNeeded message down its view hierarchy, thus redrawing all views.

Carl Norum
how do I set the length and width from this?
Matt S.
Serriously? That's the whole point. Make up an NSRect with your desired origin and size, and then send it to your window with this method. It will change size.
Carl Norum
That's what i tried but I got the error "incompatible types in assignment"
Matt S.
How are you creating the NSRect?
Abizern
NSRect *name (in the .m)
Matt S.
That's probably your problem. NSRect is s struct, not an object. You should declare it as NSRect name (note, no asterisk). The incompatible type is probably because it's expecting an NSRect and your passing in an NSRect* (a pointer to an NSRect)
Abizern
ok, I just fixed it and I'm still getting that error... wtf!
Matt S.
Try posting more code. It's hard to see what's going wrong.
Abizern
+1 for more code.
Carl Norum
All of this is within the ibaction of the window: NSString *newlen = [customlength stringValue]; NSString *newwid = [customwidth stringValue]; NSRect rect =[[self screen] frame]; rect.size.height = newlen; rect.size.width = newwid;//setFrame:(NSRect)rect;// NSRect frameRect.size.height = newlen;// NSRect frameRect.size.width = newwid;// NSLog(newlen, newwid);
Matt S.
sorry it's not formatted :(
Matt S.
`NSString`s are not numbers. Of course the `NSRect` has no idea what to do with strings. It expects `CGFloat` values for `width` and `height`.
Alex
so how should I convert them...
Matt S.
#1, Edit your question to add the code. #2, why would you use strings to do the job of numbers. Time to go back to basics, I think.
Carl Norum
I was in a rush
Matt S.
Your rush meant it took longer to sort out your problem.
Abizern
as long as it gets solved (which it did) I don't care
Matt S.
+2  A: 

NSRect is defined like this:

typedef struct _NSRect {
    NSPoint origin;
    NSSize size;
} NSRect;

And NSSize is defined like this:

typedef struct _NSSize {
    CGFloat width;
    CGFloat height;
} NSSize;

You need to convert your NSStrings to numbers first. You can do that like this:

CGFloat numericalValue = [stringValue doubleValue];

(I don't actually remember if CGFloat is defined as a float or double. I'm just too lazy to look it up right now.) Note that this will cause an exception if stringValue does not represent a properly formatted number.

Alex
thanks, now I got rid of the errors, now I just need to set that height and width to the window
Matt S.
`CGFloat` is a `float` in 32-bit and a `double` in 64-bit. But since you need to make an NSSize you will probably use `NSMakeSize` which takes floats, so try using `[customWidth floatValue]` etc.
Abizern
Yep, `NSMakeSize` would be easier and more compact. I should have mentioned that in my answer. Thanks for saving me the effort of looking up `CGFloat` :-)
Alex