0

I'm trying to make a Facebook clone for practicing iOS and I can't see why a label I have on my news feed gets unnecessary padding.

Padding on a label

It only occurs on some labels, others on my news feed turn out fine. However for a select few there's a block of white pace above and below. At first I thought it was an alignment issue so I changed the labels background to green to show that the constraints hold out.

Anyone know as to why it's placing the padding, only for a select few?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jahoe
  • 1,666
  • 2
  • 12
  • 27
  • 1
    Can you NSLog the label and post here? The output will include its frame. Somebody here (http://stackoverflow.com/questions/3476646/uilabel-text-margin) tweaked label this way, but I'd avoid that until we understand what the issue is. – danh Sep 11 '14 at 14:28
  • 2
    UIlabel will always vertically align within the given frame. So if the View has an excess height, then the text will appeard centrally aligne. You can use `sizeTofit`. This should help you out. http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel – GoodSp33d Sep 11 '14 at 14:33
  • The log of the label is: > – Jahoe Sep 11 '14 at 14:33
  • sizeToFit doesn't help I'm afraid. It also doesn't align it to the top of the label. – Jahoe Sep 11 '14 at 14:43
  • I've just realised... When I scroll down the table and back up the label turns out fine. It seems on the initial draw there is an issue but on the redraw it turns out fine? – Jahoe Sep 11 '14 at 14:56
  • @Jahoe Switch to Auto sizing, if you are not using constraints. – GoodSp33d Sep 11 '14 at 15:08

3 Answers3

2

By default UILabel will center its content vertically. Therefore, if label.bounds.size.height is greater than size of the text, the label's instinct is to center the content vertically, which will results in the vertical padding that you see in the attached image. Ensure that the label's height is being set according to the height of the text it contains and the problem should go away.

dbart
  • 5,468
  • 2
  • 23
  • 19
1

If your cell is using autolayout to determine the height of the label you should set the preferredmaxlayoutwidth to an appropriate value (for example table width) before laying out the cell.

Arash
  • 1,286
  • 1
  • 8
  • 14
1

As dbart pointed out your label's frame is likely higher than the text needed. You can fix this by calling -sizeToFit. You can also check the amount of space the label is actually using for text by using +textRectWithBounds:maximumNumberOfLines:

Rolf Hendriks
  • 411
  • 4
  • 8