How do I tell a NSTextView to update its textStorage while It is waiting for input?

When a user is editing a NSTextView and commits some kinds of operations (e.g. Quiting an app), the NSTextView's textStorage does not contain the current contents of the NSTextView. How do I get the current contents?

Replies

I'm not sure exactly what's going wrong for you, but you can force a text view to update using any of the following (depending on what actually is going wrong for you):

// asks the text view to redraw
textView.needsDisplay = true

// asks for relayout
textView.needsLayout = true

// these two need to be called together to force the NSLayoutManager to reprocess this range
textView.layoutManager?.invalidateGlyphs(
      forCharacterRange: NSRange(location: 0, length: textView.string.utf16.count),
      changeInLength: 0,
      actualCharacterRange: nil)
textView.layoutManager?.invalidateLayout(
      forCharacterRange: NSRange(location: 0, length: textView.string.utf16.count),
      actualCharacterRange: nil)