以编程方式更改NSTextView的文本

Mat*_*oal 20 macos cocoa objective-c nstextview

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

Mat*_*oal 25

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

  • @AndrewSmiley我说的是OS X而不是iOS. (3认同)

Pet*_*erg 16

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

textView.string = newString
Run Code Online (Sandbox Code Playgroud)


Dun*_*ald 6

如果您想设置属性文本(格式化文本),请尝试

[myTextView setAttributedString:(NSAttributedString*)attrString].
Run Code Online (Sandbox Code Playgroud)

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

  • 这个方法现在在 NSTextStorage 上 - 所以,在 Swift 中, `myTextView.textStorage?.setAttributedString(attrString)` (2认同)

Ram*_*uri 5

像这样的东西:

[textView setString:@"new value"];
Run Code Online (Sandbox Code Playgroud)


Iva*_*lod 5

目标 C / Xcode 9

[myTextView setString:@"The string"];
self.myTextView.string = @"My String";
Run Code Online (Sandbox Code Playgroud)

斯威夫特/ Xcode 9

self.myTextView.setString("MyString")
self.myTextView.string = "My String"
Run Code Online (Sandbox Code Playgroud)