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 structure (with NSLayoutManager
etc...), but I can't find a valid way to modify the current text
value.

- 46,283
- 15
- 111
- 140

- 16,038
- 19
- 109
- 186
6 Answers
The right method is "setString" [textView setString:@"the string"];

- 61,481
- 12
- 97
- 110

- 16,038
- 19
- 109
- 186
-
This method doesn't exist as of iOS 8 (not sure when it stopped) – AndrewSmiley Aug 29 '15 at 14:36
-
3@AndrewSmiley I'm talking about OS X not iOS. – MatterGoal Aug 29 '15 at 18:18
-
Whoops. Didn't see that tag there! – AndrewSmiley Aug 29 '15 at 21:09
Setting text on outlet textView Swift Xcode 8.0
textView.string = newString

- 1,359
- 2
- 24
- 26
Something like this:
[textView setString:@"new value"];

- 44,709
- 21
- 151
- 275

- 21,580
- 26
- 105
- 187
-
-
but... you point me to the right solution that is probably simpler than what I was trying to do. – MatterGoal Feb 01 '13 at 12:23
-
The right method is "setString" `[textView setString:@"the string"];` – MatterGoal Feb 01 '13 at 12:24
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...

- 51,061
- 28
- 99
- 211

- 8,496
- 6
- 41
- 76
-
2This method is on NSTextStorage now - so, in Swift, `myTextView.textStorage?.setAttributedString(attrString)` – Giles Sep 06 '19 at 08:36
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"

- 111
- 2
- 5
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()

- 1,329
- 9
- 14
-
I think adding `aNotification.object!.activateIgnoringOtherApps(true)` to end of `applicationDidFinishLaunching` will fix 1). Not sure about 2) – Jeff Peterson Dec 26 '15 at 13:55