0

I have a UILabel that covers most of the view. When I place text in the label, the text is center in the middle of the label. I have tried everything by playing around with the options in the attributes inspector, however the text wont start in the top area of the label, instead it appears in the middle of the label.

This is what happens

Is there any way to places the text in the top part of the label??

(If someones is wondering, yes I want the UILabel to cover most of the view because some texts are longer then others, and some are shorter, and I would prefer the text to be placed on the top part and not the middle of the UIlLabel.)

Community
  • 1
  • 1
Cristian Siles
  • 783
  • 1
  • 7
  • 13
  • use TextView Instead of Label. it will solve your problem. – Im Batman Mar 23 '16 at 03:34
  • 1
    Constrain the top of the label to the top of the view but don't constrain the bottom - allow it to expand as required to accomodate the text – Paulw11 Mar 23 '16 at 03:35
  • Paulw11's suggestion is the best solution as UITextView is slightly heavier than a plain label. – David Wong Mar 23 '16 at 03:37
  • use Size to Fit method http://stackoverflow.com/a/1054681/3800154 – Mayank Patel Mar 23 '16 at 03:44
  • If I use a TextView when a large text is displayed the textView turns scrollable and the text is displayed from a lower part instead of the top area. If I don't constraint the bottom of the UILabel and I place a new object under it I'll have a height is ambiguous warning and it might visually be different on all devices. – Cristian Siles Mar 23 '16 at 13:33
  • @MayankPatel I've seen that thread before however didn't really help me because its out dated and it isn't swift. – Cristian Siles Mar 23 '16 at 13:43

1 Answers1

1

UILabel has an intrinsicContentSize matching the text contents. If you don't constrain the height, then it will automatically adjust. So you don't need to make it cover the entire view.

Instead, tag it onto the top with a fixed distance. Specify the bottom distance with greater-than-or-equal. This way the label can grow until it reaches the lower limit and then the text will begin to be truncated.

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57