Cocoa API解説(macOS/iOS)

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。

目次

main

INDEX>AppKit>NSScreen

メインスクリーン

Swift

open class var main: NSScreen? { get } 

Objective-C

+ (NSScreen *)mainScreen

解説

メインスクリーンを返します。
メインスクリーンはメニューバーがアクティブになります。

返り値

メインスクリーン
Objective-C

+ (NSScreen *)mainScreen

Swift

NSScreen

引数

なし

フレームワーク

ApplicationKit

クラス

NSScreen

使用可能

10.0

参照

更新時のバージョン

OS X 10.14.5
Swift5.0

関連記事(外部サイト)

例文

[NSScreen mainScreen]
///
NSRect sRect = [[NSScreen mainScreen] frame];

NSLog([NSString stringWithFormat:@"mainscreen size = x:%.1f,y:%.1f,width:%.1f,height:%.1f",sRect.origin.x,sRect.origin.y,sRect.size.width,sRect.size.height]);

}

Swift5.0

    //NSScreen main
    @IBAction func function002(_ sender: Any) {
        let mainScreen : NSScreen = NSScreen.main!
        print(mainScreen.frame)
    }


Swift

    //Swift2.0
    //NSScreen mainScreen()
    //NSScreen deepestScreen()
    //NSScreen screens()
    //NSScreen supportedWindowDepths()
    //NSScreen deviceDescription
    //NSScreen visibleFrame
    //NSScreen colorSpace
    //NSScreen screensHaveSeparateSpaces
    @IBAction func function001(sender: AnyObject) {
        let theScreen : NSScreen = NSScreen.mainScreen()!
        NSLog("main screen size=  (%.2f✕%.2f)",
                                    Float(theScreen.frame.size.width),
                                    Float(theScreen.frame.size.height) )
        //-> main screen size=  (1366.00✕768.00)
        
        let deepestScreen : NSScreen = NSScreen.deepestScreen()!
        NSLog("deepestScreen screen depth= %d", NSBitsPerPixelFromDepth(deepestScreen.depth) )
        //-> deepestScreen screen depth= 24
        

        
        let supportedWindowDepths : UnsafePointer<NSWindowDepth> = theScreen.supportedWindowDepths
        
        NSLog("supportedWindowDepths = %d", NSBitsPerPixelFromDepth( supportedWindowDepths[0]) )
        //-> supportedWindowDepths = 8
        
        let screenArray : [AnyObject] = NSScreen.screens()!
        for value in screenArray {
            NSLog("screen size=  (%.2f✕%.2f)",
                Float(value.frame.size.width),
                Float(value.frame.size.height) )
        }
        //-> screen size=  (1366.00✕768.00)
        //-> screen size=  (1920.00✕1080.00)

        let descDic : [NSObject:AnyObject] = theScreen.deviceDescription
        NSLog("description = %@", descDic )
        
        //description = {
        //    NSDeviceBitsPerSample = 8;
        //    NSDeviceColorSpaceName = NSCalibratedRGBColorSpace;
        //    NSDeviceIsScreen = YES;
        //    NSDeviceResolution = "NSSize: {72, 72}";
        //    NSDeviceSize = "NSSize: {1366, 768}";
        //    NSScreenNumber = 69680256;
        //}
        
        let visibleFrame : NSRect = theScreen.visibleFrame
        NSLog("visibleFrame = (x=%.2f,y=%.2f,w=%.2f,h=%.2f)",
                                    Float(visibleFrame.origin.x),
                                    Float(visibleFrame.origin.y),
                                    Float(visibleFrame.size.width),
                                    Float(visibleFrame.size.height) )
        // -> visibleFrame = (x=0.00,y=43.00,w=1366.00,h=702.00)
        
        let colorSpace : NSColorSpace = theScreen.colorSpace!
        NSLog("colorSpace = %@",colorSpace.description)
        // -> colorSpace = Color LCD colorspace

        
        if (NSScreen.screensHaveSeparateSpaces()){
            NSLog("screensHaveSeparateSpaces = YES")
        }else{
            NSLog("screensHaveSeparateSpaces = NO")
        }
        
        let aligneRect : NSRect = theScreen.backingAlignedRect(NSMakeRect(0.3, 0.0,
            100.3, 123.5),
            //options: NSAlignmentOptions.AlignMinXInward | .AlignMinYNearest |
            //    .AlignMaxXInward | .AlignMaxYNearest )
            options: NSAlignmentOptions.AlignAllEdgesInward )
        
        
        NSLog("aligneRect = (x=%.1f,y=%.1f,w=%.1f,h=%.1f)",
            Float(aligneRect.origin.x),
            Float(aligneRect.origin.y),
            Float(aligneRect.size.width),
            Float(aligneRect.size.height) )
        
        //NSMakeRect(0.3, 0.0,105.234, 123.5),
        //NSAlignmentOptions.AlignMinXNearest | .AlignMinYNearest |.AlignMaxXNearest | .AlignMaxYNearest )
        // -> aligneRect = (x=0.00,y=0.00,w=106.00,h=124.00)

        
        
        let window : NSWindow
        = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300.0, 200.0),
            styleMask: NSTitledWindowMask
                | NSClosableWindowMask
                | NSMiniaturizableWindowMask
                | NSResizableWindowMask,
            backing: .Buffered,
            `defer`: false,
            screen: theScreen )
        windowArray.addObject(window) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
        window.center()//ウインドウをスクリーンの中心に
        window.title = "ウインドウタイトル"//タイトル設定
        window.orderFront(self)//前面に
        window.makeKeyAndOrderFront(self)//表示
    }