I have a View where I create a UILabel with numberOfLines equal to zero. I want the label size to be adjusted according to the content of my label (according to numberOfLines). However, I have tried many things including all this, and it still does not work for me. I use Neon library for AutoLayot.
And here's my whole for View:
import UIKit
import Neon
class AdditionalDescriptionView: UIView {
lazy var locationLabel = UILabel()
lazy var seasonLabel = UILabel()
lazy var quantityLabel = UILabel()
lazy var durationLabel = UILabel()
lazy var requirementsLabel = UILabel()
var height: CGFloat = 0
var text = NSString()
var size = CGSize()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup() {
locationLabel.textAlignment = .left
locationLabel.text = "Локация: Каркаралинск (200км от Караганды)"
locationLabel.textColor = .black
locationLabel.font = UIFont.avenirNextRegular(ofSize: 14)
locationLabel.numberOfLines = 0
seasonLabel.textAlignment = .left
seasonLabel.textColor = .black
seasonLabel.font = UIFont.avenirNextRegular(ofSize: 14)
seasonLabel.text = "Сезоны: все"
quantityLabel.textAlignment = .left
quantityLabel.textColor = .black
quantityLabel.text = "Количество людей: 5-25"
quantityLabel.font = UIFont.avenirNextRegular(ofSize: 14)
durationLabel.textAlignment = .left
durationLabel.textColor = .black
durationLabel.text = "Длительность тура: 3 суток"
durationLabel.font = UIFont.avenirNextRegular(ofSize: 14)
requirementsLabel.textAlignment = .left
requirementsLabel.textColor = .black
requirementsLabel.text = "Требования: удобная обувь, дополнительный груз не более 3кг, минимум 1л питьевой воды. Лицам с кардио- и дыхательными проблемами не рекомендуется участие в туре."
requirementsLabel.font = UIFont.avenirNextRegular(ofSize: 14)
requirementsLabel.numberOfLines = 0
requirementsLabel.lineBreakMode = .byWordWrapping
requirementsLabel.sizeToFit()
self.addSubview(locationLabel)
self.addSubview(seasonLabel)
self.addSubview(quantityLabel)
self.addSubview(durationLabel)
self.addSubview(requirementsLabel)
updateConstraints()
}
override func updateConstraints() {
locationLabel.anchorToEdge(.top, padding: 0, width: self.frame.width, height: AutoHeight)
seasonLabel.align(.underCentered, relativeTo: locationLabel, padding: 0, width: self.frame.width, height: AutoHeight)
seasonLabel.alignAndFillWidth(align: .underCentered, relativeTo: locationLabel, padding: 0, height: AutoHeight)
quantityLabel.alignAndFillWidth(align: .underCentered, relativeTo: seasonLabel, padding: 0, height: AutoHeight)
durationLabel.alignAndFillWidth(align: .underCentered, relativeTo: quantityLabel, padding: 0, height: AutoHeight)
// fix requirementsLabel height
requirementsLabel.alignAndFillWidth(align: .underCentered, relativeTo: durationLabel, padding: 0, height: AutoHeight)
height = locationLabel.frame.height+seasonLabel.frame.height+quantityLabel.frame.height+durationLabel.frame.height+requirementsLabel.frame.height
}
}
using this for actually adding the view:
self.view.addSubview(additional)
additional.anchorAndFillEdge(.top, xPad: 0, yPad: 0, otherSize: additional.height)
additional.updateConstraints()