macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

image

INDEX>AppKit>NSButton

ボタンに貼り付けられている画像

Objective-C

@property(strong) NSImage *image

Swift

var image: NSImage?

解説

ボタンに貼り付けられている画像

設定値

Objective-C

( NSImage * )

Swift

ボタン画像

フレームワーク

ApplicationKit

クラス

NSButton

使用可能

10.0

例文

Objective-C

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{

 //開けるファイル拡張子の配列
    NSArray      *imgTypes    = [ NSArray arrayWithObject : @"tiff" ];
    //OpenPanelを作る
    NSOpenPanel  *opImage       = [ NSOpenPanel openPanel ];
    //Imageを作る
    NSImage      *img;
    //OpenPanelの結果のボタン番号
    int		  opRet;
     
        //OpenPanelでファイル選択   
    opRet = [ opImage runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
                                     file : @"Pictures" //どのファイルを選択しておくか
                                    types : imgTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
        //NSImageを作ってファイルから読み込む
        img = [ [ NSImage alloc ] 
                          initWithContentsOfFile: [ opImage filename ] ];
//ボタンにImageをセット
        [but1 setImage : img ];
        //but1の画像を取得してbut2にセット
        [but2 setImage : [but1 image] ];
}
}

@end

Swift

// MARK: - image
    //NSButton image
    //http://cocoaapi.hatenablog.com/entry/00011103/NSButton_image
    //Swift2.0
    @IBAction func function009(sender: AnyObject) {
        //ウインドウ作成
        let aWindow : NSWindow
        = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300.0, 200.0),
            styleMask: NSTitledWindowMask
                | NSClosableWindowMask
                | NSMiniaturizableWindowMask
                | NSResizableWindowMask,
            backing: .Buffered,
            `defer`: false,
            screen: NSScreen.mainScreen())
        windowArray.addObject(aWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
        aWindow.center()//ウインドウをスクリーンの中心に
        aWindow.title = "ウインドウタイトル"//タイトル設定
        //ボタン作成
        let theButton : NSButton = NSButton(frame: NSMakeRect(70.0, 50.0, 150.0, 120.0))
        theButton.title = "Change"//タイトル
        theButton.alternateTitle = "Alter"
        
        //alternateTitleを表示するには
        //MomentaryChangeButtonである必要がある
        theButton.setButtonType(NSButtonType.MomentaryChangeButton)
        theButton.bezelStyle = NSBezelStyle.RegularSquareBezelStyle
        //スタイル
        theButton.imagePosition = NSCellImagePosition.NoImage
        //NSCellImagePosition.ImageRight 画像が右
        //ImageAbove 画像が上
        //ImageBelow 画像が下 指定しないと
        theButton.image = NSImage(named: "face")
        
        theButton.action = Selector("buttonAction002:")//ボタンを押した時に動かす関数
        theButton.target = self//ターゲット
        
        aWindow.contentView!.addSubview(theButton)
        
        aWindow.orderFront(self)//前面に
        aWindow.makeKeyAndOrderFront(self)//表示
        
        
    }

更新時バージョン

OS X 10.11.3