开发者

Centering an NSRect with code

Hey all, I have a project that I am working on and I have noticed that my NSRect that I draw at a certain x, y coordinate that would be the center for the reso开发者_如何学编程lution I am working on, is not the center if the resolution changes. I understand how it all works.

The Issue is that my project is going to be displayed on multiple resolutions does anyone know of a possible solution to centering the NSrect regardless of what aspect or resolution the screen is in. I have my classes declared as NSPanels that have custom drawing. Any ideas on a possible solution would be helpful. Thanks all.

This is the NSRect x, y, width, height

NSRect windowFrame = NSMakeRect(400, 500, 500, 100);

then

window = [[Mainbox alloc] initWithContentRect:windowFrame 
                                          styleMask:NSBorderlessWindowMask 
                                            backing:NSBackingStoreBuffered 
                                              defer:NO];


To center one rectangle within another, set the inner rect's origin to ((the outer rect's origin plus half the outer rect's size) minus half the inner rect's size).

But, you don't need to do that.

Send your window a center message before you order it in. It'll center itself HIG-appropriately.

You may want to initialize your window with a starting rect originating at 0,0 instead of some arbitrary point, in order to ensure that, if there's a main-menu screen, that's the screen the window will consider itself to be on.


It's hard to provide the following code without the myriad dependencies it requires from various bits and bites placed elsewhere in my "personal framework".. but I have an Object Class… similar I guess to what apple does when it defines a struct like NSRect, in the first place… that provides some of the convenience features you are desiring.. such as…

@interface
NSRect AGPositionRectOnRect(NSRect inner, NSRect outer, NSPoint position);
// moves the origin of the rect
NSRect AGCenterRectOnPoint(NSRect rect, NSPoint center);
// returns the innter rect with its posiion centeredn on the outer rect
NSRect AGCenterRectOnRect(NSRect inner, NSRect outer);
// will a square rect with a given center
NSRect AGSquareAround(NSPoint center, CGFloat distance);

@implementation
NSRect AGCenterRectOnRect(NSRect inner, NSRect outer) {
    return AGPositionRectOnRect(inner, outer, AGHalfPoint);
}
NSRect AGCenterRectOnPoint(NSRect rect, NSPoint center) {
    return NSMakeRect(center.x - rect.size.width  / 2, 
                center.y - rect.size.height / 2, 
                rect.size.width, 
                rect.size.height);
}
NSRect AGCenterRectOnRect(NSRect inner, NSRect outer) {
    return AGPositionRectOnRect(inner, outer, AGHalfPoint);
}
NSRect AGSquareAround(NSPoint center, CGFloat distance) {
    return NSMakeRect(center.x - distance, 
                center.y - distance, 
                2 * distance, 
                2 * distance
                );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜