【问题标题】:Change the text of a NSTextView programmatically以编程方式更改 NSTextView 的文本
【发布时间】:2013-01-16 18:10:39
【问题描述】:

我找不到任何似乎与NSTextView 中显示的文本值相关的参数。我知道NSTextView 使用复杂的结构(NSLayoutManager 等...),但我找不到修改当前text 值的有效方法。

【问题讨论】:

    标签: objective-c macos cocoa nstextview


    【解决方案1】:

    Objective C / Xcode 9

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

    Swift / Xcode 9

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

    【讨论】:

      【解决方案2】:

      在插座 textView Swift Xcode 8.0 上设置文本

      textView.string = newString
      

      【讨论】:

      • 谢谢!用过textview.string? = 新字符串
      【解决方案3】:

      差不多了 - 程序在下面 - 这几乎可以工作。那里有两个 突出问题:

      1) 在文本中设置正确的选择点需要两次鼠标点击 第一个总是到文本的末尾。所需的第二个
      位置

      2) 在 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()
      

      【讨论】:

      • 我认为将 aNotification.object!.activateIgnoringOtherApps(true) 添加到 applicationDidFinishLaunching 的末尾将修复 1)。不确定 2)
      【解决方案4】:

      类似这样的:

      [textView setString:@"new value"];
      

      【讨论】:

      • 我说的是 NSTextView 而不是 NSTextField。
      • 但是......你指出了正确的解决方案,它可能比我尝试做的更简单。
      • 正确的方法是“setString”[textView setString:@"the string"];
      【解决方案5】:

      如果你想设置属性文本(格式化文本)他们试试

      [myTextView setAttributedString:(NSAttributedString*)attrString].
      

      NSTextView 包含一个实际保存文本的 NSTextStorage 对象...

      【讨论】:

      • 这个方法现在在 NSTextStorage - 所以,在 Swift 中,myTextView.textStorage?.setAttributedString(attrString)
      【解决方案6】:

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

      【讨论】:

      • 此方法在 iOS 8 中不存在(不确定何时停止)
      • @AndrewSmiley 我说的是 OS X 而不是 iOS。
      • 哎呀。没看到那个标签!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      相关资源
      最近更新 更多