How to use PureLayout to space labels apart

283 Views Asked by At

I'm having some trouble getting PureLayout to work with UILabel's that I'm putting in my view and attempting to space apart from each other

I have one label that is spaced with some margin from the top of the view and the left of the view. Then I have a secondary label that should be also placed with some margin from the left of the view and then spaced the same amount of margin from the bottom of the first label

The autoPinEdge seems to want to UIView to reference the bound and a UILabel is obviously not a UIView so I'm trying to figure out how to get this to work

My first thought was to create new views to just hold the labels that way I can reference the views to get coordinates and such, but they aren't appearing in the view now, here is the basic setup

placeTitleLabelView = UIView(frame: CGRect.zero)
placeTitleLabel = UILabel(frame: CGRect.zero)
placeTitleLabel.textColor = UIColor.white
placeTitleLabel.font = UIFont(name: "Helvetica", size: 25)

placeTitleLabelView.addSubview(placeTitleLabel)
scrollView.addSubview(placeTitleLabelView)

placeDistanceLabelView = UIView(frame: CGRect.zero)
placeDistanceLabel = UILabel(frame: CGRect.zero)
placeDistanceLabel.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
placeDistanceLabel.font = UIFont(name: "Helvetica", size: 18)

placeDistanceLabelView.addSubview(placeDistanceLabel)
scrollView.addSubview(placeDistanceLabelView)

And then later I attempt to set up all the constraints

placeTitleLabelView.autoPinEdge(.left, to: .left, of: scrollView, withOffset: edgesInset)
placeTitleLabelView.autoPinEdge(.top, to: .top, of: scrollView, withOffset: edgesInset)

placeDistanceLabelView.autoPinEdge(.left, to: .left, of: scrollView, withOffset: edgesInset)
placeDistanceLabelView.autoPinEdge(.top, to: .bottom, of: placeTitleLabelView, withOffset: edgesInset / 2)

All the other elements I'm working with above this are showing just fine but I don't understand why these aren't, they were when they were just labels but I couldn't get the constraints to work with them.

1

There are 1 best solutions below

0
On

I was able to get these to work by making sure I set the height of the containing view to the height of the label after setting the labels size

placeDistanceLabel.sizeToFit()
placeDistanceLabelView.autoSetDimension(.height, toSize: placeDistanceLabel.bounds.height)
placeDistanceLabelView.autoPinEdge(.left, to: .left, of: scrollView, withOffset: edgesInset)
        placeDistanceLabelView.autoPinEdge(.top, to: .bottom, of: 
placeTitleLabelView, withOffset: edgesInset / 2)