Как настроить ограничения с помощью SnapKit для UITextField и UILabel? - PullRequest
0 голосов
/ 27 ноября 2018

У меня есть смещение [[TextField, макс. Доступная ширина ----] (10.0)] [Label]].Я хочу установить вывод TextFeild влево и уменьшить все доступное пространство без обрезки надписи и установить метку для закрепления вправо и получить минимальный размер подгонки.

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 ]

0 голосов
/ 27 июня 2019

Реализуйте демонстрацию ниже.

enter image description here

может автоматически увеличивать высоту с атрибутами ниже.(swift 5)

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

Одновременно можно синхронизировать высоту суперпредставления, если установить
высоту суперпредставления, такую ​​же, как у метки.

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)
    }

код загрузки: https://github.com/zgpeace/SnapkitDemo/tree/dynamicHeightLabel

0 голосов
/ 27 ноября 2018

Вам нужно

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

Также вы можете полностью удалить эти 2 строки, так как по умолчанию текстовое поле ContentHuggingPriority && ContentCompressionResistancePriority меньше, чем значение по умолчанию для метки, плюс текстовое поле не имеет собственного размера

...