• 1

I have a UILabel with space for two lines of text. Sometimes, when the text is too short, this text is displayed in the vertical center of the label.

How do I vertically align the text to always be at the top of the UILabel?

image representing a UILabel with vertically-centered text

  1. Set the new text:

    myLabel.text = @"Some Text"
  2. Set the maximum number of lines to 0 (automatic):

    myLabel.numberOfLines = 0
  3. Set the frame of the label to the maximum size:

    myLabel.frame = CGRectMake(20,20,200,800)
  4. Call sizeToFit to reduce the frame size so the contents just fit:

    [myLabel sizeToFit]

The labels frame is now just high and wide enough to fit your text. The top left should be unchanged. I have tested this only with top left aligned text. For other alignments, you might have to modify the frame afterwards.

Also, my label has word wrapping enabled.

  • 0
Reply Report

Related Questions

Trending Tags