0

I am having trouble in setting the text of a UILabel's text to be top aligned.

I have read some answers in stackoverflow and I tried: Vertically align text to top within a UILabel

self.label.text = @"Hello world";
[self.label setNumberOfLines:0];
[self.label sizeToFit];

The label's height is 67 pixel long. But when I run it, the text is vertically centered in the label. So how can I make it to top aligned?

Community
  • 1
  • 1
michael
  • 106,540
  • 116
  • 246
  • 346
  • Why do you want it to be top aligned vs. shrinking the label so it's no taller than necessary to fit the text? Are you making the label much taller than it needs to be? – rdelmar Nov 28 '14 at 22:06
  • I want to reserve space the label for multiple lines. But if there is only 1 line of text, I want to single line to be top aligned, instead of vertical center aligned. – michael Nov 28 '14 at 22:26
  • Reserve space in what sense? Space for what? If you set your label to have 0 lines, and have no fixed height for it, it will expand only as much as necessary to accommodate the text. If there is only one line, it will only be one line tall, and there will be no difference between top and center aligned. If you mean space between this label and some other view, you handle that with auto layout. – rdelmar Nov 28 '14 at 22:28

1 Answers1

0

You could just use AutoLayout to constrain the vertical position of the label. For example, you could pin the top edge of the label to the top of the superview or something.

metatation
  • 167
  • 6