我试图在NSWindow中创建一个可编辑的文本区域。到目前为止,我可以做到 一个窗口并添加一个文本字段 - 但是当我选择它并输入字符时,字符会在shell中而不是文本区域中回显。
注意:这不是Xcode项目 - 我试图在单个项目中执行此操作 shell中的文件 - 我的目标是仅在代码中执行此操作
要复制错误,请将以下代码放入文件(experiment.swift)并提供shell命令
> swift experiment.swift
这是代码
import Cocoa
class MyAppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
let ed = NSTextField(frame: NSMakeRect(20, 10, 180, 160))
func applicationDidFinishLaunching(aNotification: NSNotification) {
window.setContentSize(NSSize(width:600, height:200))
window.styleMask = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask
window.opaque = false
window.center();
window.title = "My window"
ed.font = NSFont(name:"Helvetica Bold", size:20)
ed.stringValue = "edit me"
ed.editable = true
ed.selectable = true
window.contentView!.addSubview(ed)
window.makeKeyAndOrderFront(window)
window.level = 1
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
let app = NSApplication.sharedApplication()
let obj = MyAppDelegate()
app.delegate = obj
app.run()