1

I have UIView which has a child UILabel. I have read the following SO post about aligning the text to the top of the view : Vertically align text to top within a UILabel. I see the approach discussed here in different methodologies, but I have a special case: I don't want the UILabel in my case to go past the bottom boundary of the UIView. This seems though to prevent the auto resizing of the UILabel since now it is pinned to all four directions (Trailing, Leading, Top, and Bottom). Is this impossible to achieve?

The solution presented in the above SO post suggested doing Trailing Leading and Top with the idea that it would resize the Vertical dimension based on the instrinsic size.

Thalatta
  • 4,510
  • 10
  • 48
  • 79

1 Answers1

3

All you need is a constraint for the bottom space to superview (the UIView in this case) to be greater than or equal to 0. Number of lines for the label is 0.


enter image description here

enter image description here



Using your question as text:

 @IBOutlet weak var text: UILabel!

     override func viewDidLoad() {
         super.viewDidLoad()

         text.text = "I have UIView which has a child UILabel. I have read the following SO post about aligning the text to the top of
 the view : Vertically align text to top within a UILabel. I see the
 approach discussed here in different methodologies"
     }

You get:

enter image description here

The text stops when the label hits the bottom of the UIView.

Shades
  • 5,568
  • 7
  • 30
  • 48
  • I think, bottom space should be `>= 8` – Krunal Sep 07 '17 at 16:52
  • @Krunal OP wants text to not go past the UIView, so there’s no reason for 8 – Shades Sep 07 '17 at 16:58
  • Dear, your answer is correction, there is not need to see OP's requirement. General point of view, you set padding of 8 points from Top, Left (Leading) and Right (Trailing). So you set bottom padding to `>=0`, then your text may touch to bottom line of view. But if you set bottom padding to `>= 8`, your text will set 8 points of space with bottom line of view also. – Krunal Sep 07 '17 at 17:03