How to setup constraints with SnapKit for UITextField and UILabel?

6.9k Views Asked by At

I have a [[TextField need max available width----]offset(10.0)][Label]]. I want to setup TextFeild pin to left and shrink all available space without label trimming and setup label to pin right and get minimal fit size.

lazy var textField: UITextField = {
var textField = UITextField()
textField.placeholder = "Placeholder"
textField.delegate = self
textField.borderStyle = UITextField.BorderStyle.none
textField.keyboardType = UIKeyboardType.numberPad
textField.returnKeyType = UIReturnKeyType.done
textField.setContentHuggingPriority(.defaultHigh, for: .horizontal)

return textField
}()

lazy var measureLabel: UILabel = {
var label = UILabel()
label.numberOfLines = 1
label.setContentHuggingPriority(.defaultLow, for: .horizontal)
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

return label
}()

measureLabel.snp.makeConstraints { (make) in
  make.right.equalTo(self.snp.right)
  make.centerY.equalToSuperview()
}
textField.snp.makeConstraints { (make) in
  make.left.equalToSuperview()
  make.right.equalTo(self.measureLabel.snp.left).offset(-10.0)
  make.centerY.equalToSuperview()
}
2

There are 2 best solutions below

1
On BEST ANSWER

You need

label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentCompressionResistancePriority(.required, for: .horizontal)

Also you can completely remove these 2 lines as by default textfield's ContentHuggingPriority && ContentCompressionResistancePriority is lower than the default for label , plus the textfield has no intrinsic size

0
On

Implement the a demo below.

enter image description here

label can automatically increase height with attributes below. (swift 5)

label = UILabel()
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping

It can make the height of super view synchronize at the same time, when you set
the height of super view the same as label.

label = UILabel()
    viewContainer.addSubview(label)
    label.backgroundColor = UIColor.white
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    label.text = "hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day."
    self.addSubview(label)
    label.snp.makeConstraints { (make) in
        let superView = viewContainer!
        make.left.equalTo(superView).offset(10)
        make.right.equalTo(superView).offset(-10)
        make.centerY.equalTo(superView)
    }

    viewContainer.snp.makeConstraints { (make) in
        make.centerY.equalTo(self)
        make.centerX.equalTo(self)
        make.left.equalTo(self).offset(10)
        make.right.equalTo(self).offset(-10)
        make.height.equalTo(label).offset(100)
    }

download code: https://github.com/zgpeace/SnapkitDemo/tree/dynamicHeightLabel