2017-02-18 2 views
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() 
+0

실제로 달성하려는 내용을 이해하는 데 어려움을 겪고 있지만 방망이 바로 꺼내지 만 나는 틀린 것을보고 있습니다. 'height : requirementsLabel.frame.size.height'는 그 매개 변수가 상수이기 때문에 의미가 없습니다. 당신은'sizeToFit'에서 얻은 일정한 높이를 넘기 때문에 사실 이후에 다른 어떤 변화에 대해서도 조정하지 않을 것입니다. – Dima

+0

@Dima 실제로 네온 * 라이브러리의 'AutoHeight' 속성을 사용하여 자동으로 크기를 조정해야합니다. 그러나, 그것은 나에게 단지 하나의 라인 크기를 준다. 그래서, 내가 알아야 할 재산이기 때문에 나는 거기에 쓰는 것만 큼주의를 기울이지 않았습니다. –

+0

나는 그것을 보았다. 나는 'AutoHeight'를 본다. 아직도 자세한 내용이 없으면 질문에 대한 답을 확신 할 수 없지만 사용중인 코드와 정확히 일치하도록 질문을 편집해야합니다. – Dima

답변

1

내가이 사용하는 네온을 해결하기 위해 노력 포기

그리고 여기 내 보기에 대한 전체입니다. 문제는 본질적으로 컨테이너 뷰의 구체적인 높이를 정의하는 동시에 요소를 서로 마주 보도록 앵커링하려고하는 것이 었습니다. 레이아웃 앵커를 사용하는 표준 자동 레이아웃 API 만 사용하여 결국 끝났습니다. 그런 다음 컨테이너의 너비를 지정하기 만하면됩니다. 그 높이는 포함 된 레이블의 크기에 따라 자동으로 설정됩니다. 해결책은 다음과 같습니다.

import UIKit 

class AdditionalDescriptionView: UIView { 

    lazy var locationLabel = UILabel() 
    lazy var seasonLabel = UILabel() 
    lazy var quantityLabel = UILabel() 
    lazy var durationLabel = UILabel() 
    lazy var requirementsLabel = UILabel() 
    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() { 
     self.translatesAutoresizingMaskIntoConstraints = false 
     backgroundColor = UIColor.blue 
     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 

     self.addSubview(locationLabel) 
     locationLabel.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(seasonLabel) 
     seasonLabel.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(quantityLabel) 
     quantityLabel.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(durationLabel) 
     durationLabel.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(requirementsLabel) 
     requirementsLabel.translatesAutoresizingMaskIntoConstraints = false 

     locationLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true 
     seasonLabel.topAnchor.constraint(equalTo: locationLabel.bottomAnchor).isActive = true 
     quantityLabel.topAnchor.constraint(equalTo: seasonLabel.bottomAnchor).isActive = true 
     durationLabel.topAnchor.constraint(equalTo: quantityLabel.bottomAnchor).isActive = true 
     requirementsLabel.topAnchor.constraint(equalTo: durationLabel.bottomAnchor).isActive = true 
     requirementsLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true 

     locationLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true 
     seasonLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true 
     quantityLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true 
     durationLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true 
     requirementsLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true 

     locationLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true 
     seasonLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true 
     quantityLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true 
     durationLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true 
     requirementsLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true 
    } 
} 


class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let additional = AdditionalDescriptionView() 
     self.view.addSubview(additional) 
     additional.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true 
     additional.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true 
    } 
}