简体   繁体   English

以编程方式更改NSTextView的文本

[英]Change the text of a NSTextView programmatically

I can't find any parameter that seems to be related with the text value showed in a NSTextView . 我找不到任何似乎与NSTextView显示的文本值相关的参数。 I understood that a NSTextView uses a complex structure (with NSLayoutManager etc...), but I can't find a valid way to modify the current text value. 我知道NSTextView使用复杂的结构(带有NSLayoutManager等),但是我找不到修改当前text值的有效方法。

正确的方法是“ setString” [textView setString:@"the string"];

在出口TextView上设置文本Swift Xcode 8.0

textView.string = newString

像这样:

[textView setString:@"new value"];

If you want to set attributed text (formatted text) them try 如果要设置属性文本(带格式的文本),请尝试

[myTextView setAttributedString:(NSAttributedString*)attrString].

NSTextView contains a NSTextStorage object that actually holds the text... NSTextView包含一个NSTextStorage对象,该对象实际上包含文本。

Objective C / Xcode 9 目标C / Xcode 9

[myTextView setString:@"The string"];
self.myTextView.string = @"My String";

Swift / Xcode 9 Swift / Xcode 9

self.myTextView.setString("MyString")
self.myTextView.string = "My String"

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. 1)需要两次鼠标单击才能在文本中设置正确的选择点,第一个始终到达文本的末尾。 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: 2)在外壳程序中打印一个奇怪的错误--[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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM