Ios – Top aligned UILabel, make text stick to top of label view -ios

iosiphoneobjective cxcode

Is there a way to "top align" a UILabel, that is make text stick to top of label view? As opposed to center aligned vertically, or float in the center of the view, as is the default?

Here is an image of three labels, aligned left, right and center, and a UITextView that is aligned center and top. The text of the textView sticks to the top regardless of the vertical size of the view. Can I do the same thing with a label?

enter image description here

Best Answer

There's a workaround for it:

  1. Set a height & width constraint to your UILabel with a constant <= to the maximum height|width you want.
  2. Set number of lines to zero.
  3. Set the height in storyboard to fit only one line.
  4. In your code after setting the text of UILabel call sizeToFit.

This would make your UILabel to resize within the bounds of the maximum width & height you want and text will always be top aligned.