3

I have created an NSTextView and managed to populate it using a getter for the scroll view that it is nested in, and using the .insertText() function.

How do I empty the same NSTextView? I have read the documentation and there doesn't seem to be a function .removeText(). It seems a bit weird that Apple would allow you to insert data but not remove it programmatically. I have searched high and low for answers but have come up empty handed.

jscs
  • 63,694
  • 13
  • 151
  • 195
Paulo
  • 602
  • 1
  • 8
  • 20
  • Possible duplicate of [Change the text of a NSTextView programmatically](https://stackoverflow.com/questions/14646163/change-the-text-of-a-nstextview-programmatically) – jscs Jun 23 '17 at 21:47

2 Answers2

11

You can't set the text of the text view directly, but you can set its textStorage's, getting that object's mutableString, which it inherits from NSMutableAttributedString, and then using setString(), passing an empty string.

No idea what I was thinking. Use var string { get set }, inherited from NSText.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Sorry to be a pain, could you give me an example on how to get the text from the textview? I assume after you have it in the textStorage you can simply set it to ""? – Paulo Aug 03 '14 at 19:01
  • Yes, just set the string to the empty string. – jscs Aug 03 '14 at 19:01
  • OK well I have an NSTextView. Lets call it textView. I have used the scrollview to cast a documentView. and I have a string. I have insert the text. How do I get that from the textView into the storage? Sorry I am java based. – Paulo Aug 03 '14 at 19:06
  • 5
    `textView.textStorage().mutableString().setString("")` – jscs Aug 03 '14 at 19:13
  • You sir, are amazing! Sorry that I seem simple. It just feels like a very long winded way of doing something, coming from a java background. But thank you for your help and time – Paulo Aug 03 '14 at 19:17
  • Note the way to call this has changed as of xcode 6.1.1 the new way is textView.textStorage!.mutableString.setString("") – nsij22 Dec 12 '14 at 13:40
  • in swift 2.1, the right way to do is: `textView.textStorage!.mutableString.setString("")` – Johnny Zhao Nov 01 '15 at 04:29
  • 1
    I am just doing `textView.string = ""` and works fine -- something wrong with this? – wcochran Jun 23 '17 at 20:43
  • @wcochran: Sounds perfect. Absolutely no idea what I was thinking when I wrote this answer. Can't delete it, unfortunately. Please vote for the duplicate question that I just proposed. – jscs Jun 23 '17 at 21:49
  • DO NOT USE `textView.string` directly! This breaks the undo manager. If you press CMD+Z afterwards, your app will crash. See https://stackoverflow.com/questions/20751548/strange-crash-in-uitextview-undo-on-ios-7 –  Apr 26 '20 at 18:58
-1

If you set the text on the textview directly you'll lose the ability to automatically have the undo manager pick that change up.

I've found it more reliable to first select all the text and then delete it.

[self.textInput selectAll:self];
[self.textInput delete:self]; 
Mark Bridges
  • 8,228
  • 4
  • 50
  • 65