Search code examples
iosobjective-calignmentuilabelprogrammatically-created

Text alignment from bottom to top iOS SDK


Desired label behavior

As seen on the screenshot above, each top cell has few labels. The frame of the label this question concerning is highlighted with green rectangle. It shows the title of some picture/sculpture or other exhibit, so there could be quite long strings.
I would like the text of this label to be just above author label (string with font of smaller size). So, normal text is aligned from top to bottom and if text is not enough to fill all the space, the gap is at the bottom part of the label.
For this label I would like to use the opposite approach: if text is not enough, all the text at the bottom. Is there any parameter of UILabel to implement above mentioned behaviour? Or I should manually count the amount of lines and adjust the size of the label?


Solution

  • This is fairly easy using constraints. To get the effect you desire:

    • Constrain your label's position above the author label
    • Constrain the label's width or horizontal position
    • Constrain the label to a reasonable height using a greater than or equal to
    • Either constrain the label's total height as less than or equal to, or constrain the vertical position to something greater than or equal to.

    All these constraints can be added in IB very easily, or through code. These constraints will cause the label to grow vertically to accommodate more text, and then shrink back down with less text, which will achieve the effect of having the text grow up from the bottom.