Change the text of a NSTextView programmatically

前端 未结 6 570
清歌不尽
清歌不尽 2020-12-30 23:22

I can\'t find any parameter that seems to be related with the text value showed in a NSTextView. I understood that a NSTextView uses a complex stru

6条回答
  •  旧巷少年郎
    2020-12-31 00:11

    Almost there - the program is below - this almost works. There are two outstanding problems:

    1) It takes two mouse click to set the correct selection point in the text the first always goes to the end of the text. The second to the required
    position

    2) A strange error is printed in the shell - Assertion failure in -[LUPresenter animationControllerForTerm:atLocation:options:], /SourceCache/Lookup/Lookup-160/Framework/Classes/LUPresenter.m:

      import Cocoa
    
    
      class MyAppDelegate: NSObject, NSApplicationDelegate {
    
          let window = NSWindow()
    
    
          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"
    
              let ed = NSTextView(frame: NSMakeRect(20, 10, 180, 160))
              ed.font = NSFont(name:"Helvetica Bold", size:20)
              ed.string = "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()
      app.setActivationPolicy(.Regular)
    
      let obj = MyAppDelegate()
    
      app.delegate = obj
      app.run()
    

提交回复
热议问题