title: Computing text size with NSLayoutManager
body: <p>I'm working on a view which uses TextKit framework to typeset text in columns like this:
  <img src="https://i.stack.imgur.com/X6WwL.png" alt="Column Text Layout"></p>
  
  <p>I use my <code>UIView</code>'s bounds with edge insets (black rectangle) to compute 10 <code>CGRects</code> which I then transform into <code>NSTextContainers</code> (red rectangles). In <code>drawRect:</code> I pass those to the <code>NSLayoutManager</code> which typesets and draws the glyphs for me.</p>
  
  <p>My question is: <strong>How can I compute the number of columns required?</strong> I can draw a constant number of columns but with varying text lengths, I need to adjust the number of columns programmatically.</p>
  
  <p>I found the method <code>[NSLayoutManager glyphRangeForTextContainer:]</code> which returns range of length 0 when the text container is to be left empty. Therefore, I could loop to create text containers and use this method to determine if more containers are needed. However, this method is said to be inefficient as it triggers the layout computation and I'm not happy running it in a loop perhaps hundreds of times over.</p>
  
  <p>There has to be a better way!</p>
  
  <p>Thanks for your answers, Pete.</p>