以编程方式获取Mac OS X中的屏幕大小

时间:2023-01-21 14:33:37

I am able to return the screen size using:

我可以使用:

- (void) getScreenResolution {

    NSArray *screenArray = [NSScreen screens];
    NSScreen *mainScreen = [NSScreen mainScreen];
    unsigned screenCount = [screenArray count];
    unsigned index  = 0;

    for (index; index < screenCount; index++)
    {
      NSScreen *screen = [screenArray objectAtIndex: index];
      NSRect screenRect = [screen visibleFrame];
      NSString *mString = ((mainScreen == screen) ? @"Main" : @"not-main");

      NSLog(@"Screen #%d (%@) Frame: %@", index, mString, NSStringFromRect(screenRect));
    }
}

Output:

输出:

Screen #0 (Main) Frame: {{0, 4}, {1344, 814}}

屏幕#0(主)帧:{{0,4},{1344,814}

Is there a way to format {1344, 814} to 1344x814?

有办法将{1344,814}格式化为1344x814吗?


Edit:

This works perfectly:

这是完美的:

- (NSString*) screenResolution {

    NSRect screenRect;
    NSArray *screenArray = [NSScreen screens];
    unsigned screenCount = [screenArray count];
    unsigned index  = 0;

    for (index; index < screenCount; index++)
    {
        NSScreen *screen = [screenArray objectAtIndex: index];
        screenRect = [screen visibleFrame];
    }

    return [NSString stringWithFormat:@"%.1fx%.1f",screenRect.size.width, screenRect.size.height];
}

6 个解决方案

#1


11  

NSLog(@"%fx%f",screenRect.size.width, screenRect.size.height);

NSLog(@ % fx % f,screenRect.size。宽度,screenRect.size.height);

#2


14  

Finding the screen size in Mac OS is very simple:

在Mac OS中找到屏幕尺寸非常简单:

NSRect e = [[NSScreen mainScreen] frame];
H = (int)e.size.height;
W = (int)e.size.width;

#3


12  

In Swift 4.0 you can get the screen size of the main screen:

在Swift 4.0中,你可以获得主屏幕的屏幕尺寸:

if let screen = NSScreen.main {
    let rect = screen.frame
    let height = rect.size.height
    let width = rect.size.width
}

If you look for the size of the screen with a particular existing window you can get it with:

如果你使用特定的现有窗口查找屏幕的大小,你可以使用:

var window: NSWindow = ... //The Window laying on the desired screen
var screen = window.screen!
var rect = screen.frame
var height = rect.size.height
var width = rect.size.width

#4


6  

For those guy who are looking for a way to get screen resolution:

对于那些想要得到屏幕分辨率的人:

If you are programming a window based app, you can simply get the resolution from _window.screen.frame.size

如果您正在编写一个基于窗口的应用程序,那么只需从_window.screen.frame.size中获得解析

#5


2  

NSScreen.mainScreen()!.frame         // {x 0 y 0 w 1,920 h 1,200}
NSScreen.mainScreen()!.frame.width   // 1,920.0
NSScreen.mainScreen()!.frame.height  // 1,200.0

#6


0  

Swift 3 solution:

斯威夫特3解决方案:

NSScreen.main()!.frame

NSScreen.main()! .frame

#1


11  

NSLog(@"%fx%f",screenRect.size.width, screenRect.size.height);

NSLog(@ % fx % f,screenRect.size。宽度,screenRect.size.height);

#2


14  

Finding the screen size in Mac OS is very simple:

在Mac OS中找到屏幕尺寸非常简单:

NSRect e = [[NSScreen mainScreen] frame];
H = (int)e.size.height;
W = (int)e.size.width;

#3


12  

In Swift 4.0 you can get the screen size of the main screen:

在Swift 4.0中,你可以获得主屏幕的屏幕尺寸:

if let screen = NSScreen.main {
    let rect = screen.frame
    let height = rect.size.height
    let width = rect.size.width
}

If you look for the size of the screen with a particular existing window you can get it with:

如果你使用特定的现有窗口查找屏幕的大小,你可以使用:

var window: NSWindow = ... //The Window laying on the desired screen
var screen = window.screen!
var rect = screen.frame
var height = rect.size.height
var width = rect.size.width

#4


6  

For those guy who are looking for a way to get screen resolution:

对于那些想要得到屏幕分辨率的人:

If you are programming a window based app, you can simply get the resolution from _window.screen.frame.size

如果您正在编写一个基于窗口的应用程序,那么只需从_window.screen.frame.size中获得解析

#5


2  

NSScreen.mainScreen()!.frame         // {x 0 y 0 w 1,920 h 1,200}
NSScreen.mainScreen()!.frame.width   // 1,920.0
NSScreen.mainScreen()!.frame.height  // 1,200.0

#6


0  

Swift 3 solution:

斯威夫特3解决方案:

NSScreen.main()!.frame

NSScreen.main()! .frame