title: Scale text to fit in a box
body: <p>I'm working on a matlab GUI and I am trying to make the text font size resize to boxes. Currently I have a few solutions, but they aren't optimal. The fastest one is scaling the text by a goal/Extent factor:</p>
  
  <pre><code>goal = [0 0 .9 .9].*b.Position
  fs = b.FontSize*min(goal./b.Extent)
  b.FontSize = fs;
  </code></pre>
  
  <p>b is the uicontrol element. This works, but if the text is too long and needs to wrap it won't, and it will rather shrink the height until it fits on one line in the box. </p>
  
  <p>I also have a while loop approach that * or /s by .99 until extent and goal are close enough. </p>
  
  <p>What I've been looking for is an approach that will wrap the text so that I display the whole string but also take advantage of the whole space. I also had a recursive backtracking attempt that would try to find the best ratio the words would have to the ratio of the sides of the box, but that takes <code>O(2^n)</code> which is good to avoid. </p>
  
  <p>Online I found the <code>textwrap</code> function, but I think that depends on font size, so it doesn't help me. Any ideas on how to do this?</p>