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
2009-10-12 17:27:08
how do I set the length and width from this?
Matt S.
2009-10-12 18:00:43
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
2009-10-12 18:02:47
That's what i tried but I got the error "incompatible types in assignment"
Matt S.
2009-10-12 19:11:49
How are you creating the NSRect?
Abizern
2009-10-12 19:24:34
NSRect *name (in the .m)
Matt S.
2009-10-12 19:55:08
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
2009-10-12 20:18:04
ok, I just fixed it and I'm still getting that error... wtf!
Matt S.
2009-10-12 20:34:34
Try posting more code. It's hard to see what's going wrong.
Abizern
2009-10-12 20:37:37
+1 for more code.
Carl Norum
2009-10-12 20:40:29
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.
2009-10-12 20:41:00
sorry it's not formatted :(
Matt S.
2009-10-12 20:41:34
`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
2009-10-12 20:47:32
so how should I convert them...
Matt S.
2009-10-12 20:49:55
#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
2009-10-12 20:56:48
I was in a rush
Matt S.
2009-10-12 21:01:54
Your rush meant it took longer to sort out your problem.
Abizern
2009-10-12 21:06:55
as long as it gets solved (which it did) I don't care
Matt S.
2009-10-12 21:10:35
+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 NSString
s 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
2009-10-12 20:54:32
thanks, now I got rid of the errors, now I just need to set that height and width to the window
Matt S.
2009-10-12 21:01:01
`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
2009-10-12 21:05:56
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
2009-10-12 21:17:15