title: Strings container size, calculating minimum width given maximum height
body: <p>Most needs for fitting a formatted string require calculating a the <strong>height</strong> of a container given a  maximum width to for the string into.  Such functions in cocoa API reflect this e.g.</p>
  
  <pre><code>NSTextField.sizeThatFits()
  </code></pre>
  
  <p>This method will take the maximum width as fixed and adjust the height to contain the text.</p>
  
  <p>How would you do the opposite with the above - given a height limit, calculate the smallest <strong>width</strong> to enclose the text?</p>
  
  <p>One way I've first thought of is by first creating an estimate by dividing the area of a single line of text by the required height to get an estimated width. But this will always be an overestimation of the height, because words will wrap, pushing other words along and down. The next stage of the algorithm, takes this excess height and adds it onto the end of the imaginary rectangle, until the maximum height limit is broken, giving an upper and lower estimate of the width. The final part of the algorithm converges with a kind of binary search until a certain pixel tolerance is reached.  I will post this as an answer when I can.</p>
  
  <p>I feel there must be a better way of doing this? (ObjC or Swift)</p>