gpt4 book ai didi

swift - 如何在 swift shell 应用程序中制作可编辑的文本字段

转载 作者:IT王子 更新时间:2023-10-29 05:37:10 25 4
gpt4 key购买 nike

我正在尝试在 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()

最佳答案

app.run()之前,添加

app.setActivationPolicy(.Regular)

根据 the docs ,默认的激活策略是 Prohibited:

  • Prohibited

The application does not appear in the Dock and may not create windows or be activated. [...] This is also the default for unbundled executables that do not have Info.plists.

关于swift - 如何在 swift shell 应用程序中制作可编辑的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34470718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com