36

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}}

Is there a way to format {1344, 814} to 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];
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • 2
    I suggest using fast enumeration instead of indexes for the loop. It will make it both faster and easier to read. Also, `visibleFrame` is not the same thing as `frame`; `visibleFrame` excludes the region occupied by the Dock or (when the Dock is hidden) the show-Dock trigger region, plus the menu bar. – Peter Hosey Feb 13 '11 at 06:44
  • Your code may be much more simple. First of all `for (NSScreen *screen in [NSScreen screens])`. – Vladimir Prudnikov Apr 03 '13 at 23:33
  • Is there a way to get the screen size in real-life measurements? That is, in centimeters or inches? – adib Dec 09 '13 at 15:47
  • You will need to covert pixels to whatever you want. Should be easy enough. Just use the method above to get pixels, then covert. – WrightsCS Dec 09 '13 at 17:44

6 Answers6

27

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

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
j.s.com
  • 1,422
  • 14
  • 24
20

Finding the screen size in Mac OS is very simple:

NSRect e = [[NSScreen mainScreen] frame];
H = (int)e.size.height;
W = (int)e.size.width;
mamady
  • 231
  • 2
  • 4
15

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

GWW
  • 43,129
  • 11
  • 115
  • 108
  • 3
    Sweet, `return [NSString stringWithFormat:@"%.0fx%.0f",screenRect.size.width, screenRect.size.height];` worked perfectly to return **1024x768**, thanks. – WrightsCS Feb 13 '11 at 06:00
11

edit/update

Swift 4

NSScreen.main?.frame         // {x 0 y 0 w 1,920 h 1,200}
NSScreen.main?.frame.width   // 1,920.0
NSScreen.main?.frame.height  // 1,200.0 

Swift 3.x

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

Swift 2.x

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
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
7

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

Jashengmato
  • 71
  • 1
  • 3
2

Swift 3 solution:

NSScreen.main()!.frame

Aron Gates
  • 21
  • 2