Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Refering to the extension solution:</p> <pre><code>for(int i=1; i&lt; newLinesToPad; i++) self.text = [self.text stringByAppendingString:@"\n"]; </code></pre> <p>should be replaced by </p> <pre><code>for(int i=0; i&lt;newLinesToPad; i++) self.text = [self.text stringByAppendingString:@"\n "]; </code></pre> <p>Additional space is needed in every added newline, because iPhone <code>UILabels</code>' trailing carriage returns seems to be ignored :(</p> <p>Similarly, alignBottom should be updated too with a <code>@" \n@%"</code> in place of <code>"\n@%"</code> (for cycle initialization must be replaced by "for(int i=0..." too).</p> <p>The following extension works for me:</p> <pre><code>// -- file: UILabel+VerticalAlign.h #pragma mark VerticalAlign @interface UILabel (VerticalAlign) - (void)alignTop; - (void)alignBottom; @end // -- file: UILabel+VerticalAlign.m @implementation UILabel (VerticalAlign) - (void)alignTop { CGSize fontSize = [self.text sizeWithFont:self.font]; double finalHeight = fontSize.height * self.numberOfLines; double finalWidth = self.frame.size.width; //expected width of label CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode]; int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height; for(int i=0; i&lt;newLinesToPad; i++) self.text = [self.text stringByAppendingString:@"\n "]; } - (void)alignBottom { CGSize fontSize = [self.text sizeWithFont:self.font]; double finalHeight = fontSize.height * self.numberOfLines; double finalWidth = self.frame.size.width; //expected width of label CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode]; int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height; for(int i=0; i&lt;newLinesToPad; i++) self.text = [NSString stringWithFormat:@" \n%@",self.text]; } @end </code></pre> <p>Then call <code>[yourLabel alignTop];</code> or <code>[yourLabel alignBottom];</code> after each yourLabel text assignment.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload