OSXアプリをSwiftで作成しております。
※Swift3,Xcode8.1
NSViewの中央にNSTextFieldを配置する際、x座標、y座標に小数点数(サンプル2,サンプル4)を設定した場合、NStextFieldの位置が中央からズレてしまいます。
x軸、y軸には整数しか設定できないのでしょうか。
ご教示お願い致します。
import Cocoa class ViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() // サンプル1 let sampleView1 = NSView(frame: NSRect(x: 155, y: 124, width: 20, height: 25)) sampleView1.wantsLayer = true sampleView1.layer? = CALayer() sampleView1.layer?.backgroundColor = NSColor.blue.cgColor let sampleTextField1 = NSTextField(frame: NSRect(x: 1, y: 1, width: 18, height: 23)) sampleTextField1.drawsBackground = true sampleTextField1.backgroundColor = NSColor.white sampleTextField1.isEditable = false sampleTextField1.isBordered = false sampleView1.addSubview(sampleTextField1) // サンプル2 let sampleView2 = NSView(frame: NSRect(x: 185, y: 124, width: 20, height: 25)) sampleView2.wantsLayer = true sampleView2.layer? = CALayer() sampleView2.layer?.backgroundColor = NSColor.blue.cgColor let sampleTextField2 = NSTextField(frame: NSRect(x: 1.5, y: 1.5, width: 17, height: 22)) sampleTextField2.drawsBackground = true sampleTextField2.backgroundColor = NSColor.white sampleTextField2.isEditable = false sampleTextField2.isBordered = false sampleView2.addSubview(sampleTextField2) // サンプル3 let sampleView3 = NSView(frame: NSRect(x: 215, y: 124, width: 20, height: 25)) sampleView3.wantsLayer = true sampleView3.layer? = CALayer() sampleView3.layer?.backgroundColor = NSColor.blue.cgColor let sampleTextField3 = NSTextField(frame: NSRect(x: 2, y: 2, width: 16, height: 21)) sampleTextField3.drawsBackground = true sampleTextField3.backgroundColor = NSColor.white sampleTextField3.isEditable = false sampleTextField3.isBordered = false sampleView3.addSubview(sampleTextField3) // サンプル4 let sampleView4 = NSView(frame: NSRect(x: 245, y: 124, width: 20, height: 25)) sampleView4.wantsLayer = true sampleView4.layer? = CALayer() sampleView4.layer?.backgroundColor = NSColor.blue.cgColor let sampleTextField4 = NSTextField(frame: NSRect(x: 2.5, y: 2.5, width: 15, height: 20)) sampleTextField4.drawsBackground = true sampleTextField4.backgroundColor = NSColor.white sampleTextField4.isEditable = false sampleTextField4.isBordered = false sampleView4.addSubview(sampleTextField4) self.view.addSubview(sampleView1) self.view.addSubview(sampleView2) self.view.addSubview(sampleView3) self.view.addSubview(sampleView4) } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/20 12:30