0

This post is to complete one I made a while ago titled Text padding on UILabel, left with no answer.

The screenshot below shows it all. The first label, though the shortest is truncated. The second and the third even more have far too wide spaces on the right side. I want to solve those 2 problems.

enter image description here

I made a tiny project and provide it on GitHub to simply illustrate the issu and give other people the opportunity to easily take a close look.

Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

0

Please update your NSLayoutConstraint like this

view.addConstraints([
                NSLayoutConstraint(item: label,
                                   attribute: .left,
                                   relatedBy: .lessThanOrEqual,
                                   toItem: view,
                                   attribute: .left,
                                   multiplier: 1.0,
                                   constant: sideMargin),
                NSLayoutConstraint(item: label,
                                   attribute: .right,
                                   relatedBy: .lessThanOrEqual,
                                   toItem: view,
                                   attribute: .right,
                                   multiplier: 1.0,
                                   constant: sideMargin * -2),
                NSLayoutConstraint(item: label,
                                   attribute: .top,
                                   relatedBy: .equal,
                                   toItem: topView,
                                   attribute: firstLoop ? .top : .bottom,
                                   multiplier: 1.0,
                                   constant: sideMargin),
                NSLayoutConstraint(item: label,
                                   attribute: .centerX,
                                   relatedBy: .equal,
                                   toItem: view,
                                   attribute: .centerX,
                                   multiplier: 1,
                                   constant: 0)
                ])

After Correction

  • Thanks, but those constraints do not reflect what I want. Why .centerX ? (The X-position of the label is fine!). My left constraint is also fine the way it is. I only have a problem with the text inside the label not with the position of the label inside the view. – Michel Nov 15 '17 at 06:49
  • @Michel I can't say why .centerX exactly but i rectified your truncated problem by using .centerX constraint and modified the left constraint to .equal to .lessThanOrEqual constraint. See my updated post that i have attached a screenshot. – Mahesh Prasad Mahalik Nov 15 '17 at 07:16
  • Well, you fixed the truncated problem indeed. But you throw away the constraints I want. So this is not really a solution for my needs. I want to know why what I do does not work all the time. – Michel Nov 15 '17 at 08:14