在 Swift 中为 OS X 以编程方式构建 UI(没有 Interface Builder)
Build UI programmatically for OS X in Swift (without Interface Builder)
我是 Swift 的新手,我想使用 AppCode 开发一个 OS X 应用程序(我不想使用 XCode),并且没有故事板。所以我需要构建 UI 并以编程方式管理网点和操作。
我正在寻找 link 到 "Hello World" 的示例代码/教程视频或以编程方式创建 UI 的示例应用程序。感谢帮助。谢谢
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let newWindow = NSWindow(contentRect: NSMakeRect(0, 0, NSScreen.mainScreen()!.frame.width/2, NSScreen.mainScreen()!.frame.height/2), styleMask: NSTitledWindowMask|NSResizableWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask, backing: NSBackingStoreType.Buffered, defer: false)
let newText = NSTextField(frame: NSMakeRect(0, NSScreen.mainScreen()!.frame.height/4, NSScreen.mainScreen()!.frame.width/2, 40))
let myView = NSView(frame: NSMakeRect(0, 0, NSScreen.mainScreen()!.frame.width/2, NSScreen.mainScreen()!.frame.height/2))
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
newText.font = NSFont(name: "Arial Black", size: 24)
newText.backgroundColor = NSColor.clearColor()
newText.bordered = false
newText.textColor = NSColor.whiteColor()
newText.alignment = .CenterTextAlignment
newText.stringValue = "Hello World"
newText.selectable = false
newWindow.opaque = false
newWindow.movableByWindowBackground = true
newWindow.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.7)
newWindow.title = "Hello World"
newWindow.center()
newWindow.contentView.addSubview(myView)
myView.addSubview(newText)
newWindow.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
我是 Swift 的新手,我想使用 AppCode 开发一个 OS X 应用程序(我不想使用 XCode),并且没有故事板。所以我需要构建 UI 并以编程方式管理网点和操作。 我正在寻找 link 到 "Hello World" 的示例代码/教程视频或以编程方式创建 UI 的示例应用程序。感谢帮助。谢谢
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let newWindow = NSWindow(contentRect: NSMakeRect(0, 0, NSScreen.mainScreen()!.frame.width/2, NSScreen.mainScreen()!.frame.height/2), styleMask: NSTitledWindowMask|NSResizableWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask, backing: NSBackingStoreType.Buffered, defer: false)
let newText = NSTextField(frame: NSMakeRect(0, NSScreen.mainScreen()!.frame.height/4, NSScreen.mainScreen()!.frame.width/2, 40))
let myView = NSView(frame: NSMakeRect(0, 0, NSScreen.mainScreen()!.frame.width/2, NSScreen.mainScreen()!.frame.height/2))
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
newText.font = NSFont(name: "Arial Black", size: 24)
newText.backgroundColor = NSColor.clearColor()
newText.bordered = false
newText.textColor = NSColor.whiteColor()
newText.alignment = .CenterTextAlignment
newText.stringValue = "Hello World"
newText.selectable = false
newWindow.opaque = false
newWindow.movableByWindowBackground = true
newWindow.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.7)
newWindow.title = "Hello World"
newWindow.center()
newWindow.contentView.addSubview(myView)
myView.addSubview(newText)
newWindow.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}