macos 在 Mac OS X 中以编程方式获取屏幕大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4982656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically get screen size in Mac OS X
提问by WrightsCS
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];
}
采纳答案by GWW
NSLog(@"%fx%f",screenRect.size.width, screenRect.size.height);
NSLog(@"%fx%f",screenRect.size.width, screenRect.size.height);
回答by mamady
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;
回答by j.s.com
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
回答by Jashengmato
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 获取分辨率
回答by Leo Dabus
edit/update
编辑/更新
Swift 4
斯威夫特 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
斯威夫特 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
斯威夫特 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
回答by Aron Gates
Swift 3 solution:
斯威夫特 3 解决方案:
NSScreen.main()!.frame
NSScreen.main()!.frame