objective-c macos cocoa nstextview

objective c - Cambia el texto de un NSTextView programáticamente



objective-c macos (6)

Algo como esto:

[textView setString:@"new value"];

No puedo encontrar ningún parámetro que parezca estar relacionado con el valor del texto mostrado en un NSTextView . Comprendí que un NSTextView usa una estructura compleja (con NSLayoutManager etc.), pero no puedo encontrar una manera válida de modificar el valor actual del text .


Casi allí, el programa está abajo, esto casi funciona. Hay dos problemas pendientes:

1) Se requieren dos clics del mouse para establecer el punto de selección correcto en el texto, el primero siempre va al final del texto. El segundo al requerido
posición

2) Se imprime un error extraño en el shell - Error de aserción en - [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()


Configuración de texto en el texto de salidaVer Swift Xcode 8.0

textView.string = newString


El método correcto es "setString" [textView setString:@"the string"];


Si desea configurar el texto atribuido (texto formateado) inténtelo

[myTextView setAttributedString:(NSAttributedString*)attrString].

NSTextView contiene un objeto NSTextStorage que realmente contiene el texto ...


Objetivo C / Xcode 9

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

Swift / Xcode 9

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