title: UILabel and numberOfLines and sizeToFit:
body: <p>On iOS 5.</p>
  
  <p>I have a UILabel element that is originally placed in a nib.  I want the x placement to stay fixed.  I want it to take either 1 line, or 2 lines.  If more than two lines, it should use the line break setting to show the ellipsis. </p>
  
  <p>I use <code>numberOfLines</code> property and <code>-sizeToFit</code></p>
  
  <p>If I set the UILabel's <code>numberOfLines</code> property to 0, it will correctly see that for some text, there is not enough room and will wrap it to a second line after calling <code>-sizeToFit</code> but in the rare case that the line is long enough to stretch to 3 lines, I get three lines, which I don't want.  If I set <code>numberOfLines</code> property to 2, it actually stretches the whole thing out onto one line and elongates my initial frame set in the nib to be much wider.</p>
  
  <pre><code> CGRect titleFrame = [[self titleLabel] frame];
   [[self titleLabel] setNumberOfLines:0];
   [[self titleLabel] setText:newProductTitleText];
   [[self titleLabel] sizeToFit];
   CGRect newTitleFrame = [[self titleLabel] frame];
  </code></pre>
  
  <p>The CGRect are just there for me to be able to calculate things after the fact.  So setting <code>numberOfLines</code> to 0 works, and will not change the original origin.x in the frame, and will break long text into multiple lines, but will not constrain it to 2 lines.  Setting <code>numberOfLines</code> property to 2, which, when I read the Apple docs</p>
  
  <blockquote>
    <p>This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.</p>
  </blockquote>
  
  <p>It seems I should be able set this to two and still have it work.  I would expect <code>sizeToFit</code> to expand in a positive X and Y direction when expanding to fit all the text but it is expanding in a negative X direction when <code>numberOfLines</code> is set to other than 0.</p>
  
  <p>ETA:  the "Autosize" struts are set to upper and left to fix it at a min x,y.</p>
  
  <p>Thanks for any insight.</p>