3

I have an UILabel which has three constraints - top, left and right. It has some unnecessary space at the top and bottom. Do we've anyway to get rid of that space without setting a height constraint as I want height based on a font size? I'm attaching the image for reference. I want label text aligned with edges without any space or padding. I've tried many solutions from StackOverflow but nothing worked.

Any help will be appreciated. Thanks!

enter image description here

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • That's not *"unnecessary space"* ... that's for the ascenders and descenders. If you want to clip to the **glyphs** you'll need to use Core Text and work with the glyph data. – DonMag Mar 06 '21 at 20:46
  • Take a look at this q&a: https://stackoverflow.com/questions/21443625/core-text-calculate-letter-frame-in-ios – DonMag Mar 06 '21 at 20:47

1 Answers1

0

You should set more than 251 huggingPriority value for a specific axis for your label.

To do it

  1. you should use the following method if you created label from code:

    func setContentHuggingPriority(_ priority: UILayoutPriority, for axis: NSLayoutConstraint.Axis)

  2. or you can set huggingPriority in Interface Builder in Size Inspector tab if you created label from InterfaceBuilder enter image description here