0
보기 여기서 numberOfLines이 0 인 UILabel을 만듭니다. 내 라벨의 내용에 따라 라벨 크기를 조정하고 싶습니다 (numberOfLines에 따라). 그러나, 나는 모든 것을 포함하여 많은 것을 시도했다 this, 아직도 저를 위해 작동하지 않는다. AutoLayot 용 라이브러리는 Neon입니다. 실제로보기를 추가하기위한이 사용동적으로 줄 수에 따라 프로그래밍 방식으로 UILabel 크기를 조정하십시오.
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
}
}
:
self.view.addSubview(additional)
additional.anchorAndFillEdge(.top, xPad: 0, yPad: 0, otherSize: additional.height)
additional.updateConstraints()
실제로 달성하려는 내용을 이해하는 데 어려움을 겪고 있지만 방망이 바로 꺼내지 만 나는 틀린 것을보고 있습니다. 'height : requirementsLabel.frame.size.height'는 그 매개 변수가 상수이기 때문에 의미가 없습니다. 당신은'sizeToFit'에서 얻은 일정한 높이를 넘기 때문에 사실 이후에 다른 어떤 변화에 대해서도 조정하지 않을 것입니다. – Dima
@Dima 실제로 네온 * 라이브러리의 'AutoHeight' 속성을 사용하여 자동으로 크기를 조정해야합니다. 그러나, 그것은 나에게 단지 하나의 라인 크기를 준다. 그래서, 내가 알아야 할 재산이기 때문에 나는 거기에 쓰는 것만 큼주의를 기울이지 않았습니다. –
나는 그것을 보았다. 나는 'AutoHeight'를 본다. 아직도 자세한 내용이 없으면 질문에 대한 답을 확신 할 수 없지만 사용중인 코드와 정확히 일치하도록 질문을 편집해야합니다. – Dima