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
?
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]; }
On your Mac, choose Apple menu > System Preferences, click Displays , then click Display Settings. Select your display in the sidebar, then do one of the following, depending on your display: Click the Scaled pop-up menu, then choose a scaled resolution for the display.
The size of a desktop computer monitor is determined by physically measuring the screen. Using a measuring tape, start at the top-left corner and pull it diagonally to the bottom-right corner. Be sure to only measure the screen; do not include the bezel (the plastic edge) around the screen.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With