iosswiftuitextviewcore-textnstextstorage

UITextView backed by NSTextStorage not updated when text appended


The following is a trivial example of a UITextView backed by a NSTextStorage. The "doAction1" is fired by clicking on some UI button. Reading through the docs, I was under the impression that by updating the NSTextStorage in a begin/end Editing block, the View will automatically update itself as well.

However, it doesn't seems to happen -- the example below appends "...World!" to NSTextStorage but the UITextView doesn't reflect that -- and continues to just display "Hello". Any idea?

import UIKit

class ViewController: UIViewController {
  let TEXT = "Hello"

  var m_layoutManager: NSLayoutManager!
  var      m_textView: UITextView!

  @IBAction func doAction1(_ sender: AnyObject) {
      let ts = m_layoutManager.textStorage

      ts?.beginEditing()
      ts?.append(AttributedString(string:"...World!"))
      ts?.endEditing()

      // These two don't make a difference either:
      //
      // m_layoutManager.invalidateDisplay(forCharacterRange: NSRange(location: 0, length: (ts?.length)!))
      // m_textView.setNeedsDisplay()
      //
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    let textStorage = NSTextStorage(string: TEXT)
    let textContainer = NSTextContainer(
        size: CGSize(width: 200, height: 300))

    m_layoutManager = NSLayoutManager()
    m_layoutManager.textStorage = textStorage
    m_layoutManager.addTextContainer(textContainer)

    m_textView = UITextView(
        frame: CGRect(x: 0, y: 20, width: 200, height: 300),
        textContainer: textContainer)

    view.addSubview(m_textView)
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
}

Solution

  • Solved. The NSLayoutManager wasn't properly connected to the NSTextStorage. So instead of telling the layout manager that it has a storage, i.e:

    m_layoutManager.textStorage = textStorage
    

    I'm telling the storage that it has a manager:

    textStorage.addLayoutManager(m_layoutManager)
    

    And that's it.