1

I'm currently getting extra space on the top and bottom of my UILabel. I have it set to be font size 24, but it can go as small as size 14 (this is to help the text fit when used on smaller phones like 4s) And I have the number of lines constrained to just 7. Other than that, I don't have any constraint forcing it to be a certain height, I'm letting Xcode decide that, but I don't understand why it's adding the extra space. See images for all the settings I have.

UILabel Attributes

enter image description here

Size Inspector Constraints

enter image description here

View in Xcode

enter image description here

Results in iOS

enter image description here

I do have code that is rounding the corners and adding a shadow, but I don't think it is affecting the top and bottom gaps since that is showing in Xcode before the code even runs.

And just as an explanation, the constraints with 16 are making the width match the width of the white background. And the constraints with the 6 are sizing the dark green background around the UILabel.

EDIT: Adding Full Constraint list

enter image description here

Edit 2: Adding XIB image

enter image description here

Jason Brady
  • 1,560
  • 1
  • 17
  • 40
  • You have pinned the label to TOP and BOTTOM of view. Hence, the label is doing the right thing by adjusting its height according to the view. If you want to make the label adjust its height automatically, then you need different set of constraints. – Harsh Aug 11 '16 at 16:53
  • That UIView's height and width isn't set (the dark green UIView). Its size is solely determined by the size of the UILabel + 6. – Jason Brady Aug 11 '16 at 16:58
  • Can you show us all the constraints which you have added. – Harsh Aug 11 '16 at 16:59
  • Harsh: I added the full list – Jason Brady Aug 11 '16 at 17:08

2 Answers2

1

Try removing the constraints to bottom and let the UILabel to decide the height for you.

With UILabel you can't have a vertical alignment to top, this answer could help you to understand: https://stackoverflow.com/a/1054681/5109911

Community
  • 1
  • 1
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
  • I don't believe I have any bottom constraints that are forcing a specific height for the label. The only thing constrained to the bottom is the background UIView (Dark Green). And that UIView size is 100% determined by the UILabel. I did test what you suggested though, and the label still adds the spaces on top and bottom. – Jason Brady Aug 11 '16 at 17:08
  • Your problem is that the UILabel has a height greater then his content. I see a constraint to buttom with a constant of -6, try to disable it. – Marco Santarossa Aug 11 '16 at 17:11
  • You are correct that the bottom -6 constraint is there. But that UIView is sized by the UILabel. However, I did test what you suggested and didn't see any change in the UILabel extra spaces. As far as I can tell, I don't have anything forcing a height for the UILabel. – Jason Brady Aug 11 '16 at 17:14
  • Can you please share your xib file? So I can check all constraints, with the image is quite difficult – Marco Santarossa Aug 11 '16 at 17:22
  • I'll chime in here as an iOS developer for 14 years and auto layout since it came out, Apple has always left this bug, it is related to minimum size or minimum font scale (whichever you are choosing to use). In your example, your font size is 24 and minimum is set at 14, therefore you will see about 10/24ths extra space divided between top and bottom. I have never found a way around this, and have been trying for years. Literally have to rebuild everything and manually adjust font yourself and avoid minimum font size and scale. – Kevin Teman Mar 20 '23 at 22:58
-2

As mentioned in my comment, iOS developer for 14 years and auto layout since it came out, Apple has always left this bug, it is related to minimum size or minimum font scale (whichever you are choosing to use). In your example, your font size is 24 and minimum is set at 14, therefore you will see about 10/24ths extra space divided between top and bottom. I have never found a way around this, and have been trying for years. Literally have to rebuild everything and manually adjust font yourself and avoid minimum font size and scale. I recall a hilarious experiment to use Chat GPT to figure out a solution, Chat GPT started by trying to tell me all the things I'm probably doing wrong, then ended up saying "you're right, this is probably an Apple bug". Hilarious, that it came around to the true understanding: Apple is not fixing everything.

Kevin Teman
  • 85
  • 1
  • 4