Jason Brady
Jason Brady

Reputation: 1728

Extra space on top and bottom of UILabel in Xcode 7.3.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

Upvotes: 1

Views: 3525

Answers (2)

Kevin Teman
Kevin Teman

Reputation: 125

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.

Upvotes: -2

Marco Santarossa
Marco Santarossa

Reputation: 4066

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

Upvotes: 1

Related Questions