2017-02-18 39 views
0

我有一個視圖其中I創建的UILabel與numberOfLines等於零。我希望根據標籤的內容(根據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我實際上一直在使用* Neon *庫的'AutoHeight'屬性來「自動」調整大小。但是,它只給我一個線路大小。所以,我只是沒有太注意我在那裏寫的東西,因爲那是我需要查明的財產。 –

+0

我查了一下,我看到'AutoHeight'。如果沒有更多的細節,仍然不確定你的問題的答案,但你應該編輯你的問題,以確切地使用你正在使用的代碼。 – Dima

回答

1

我放棄了試圖解決這個使用霓虹燈。問題主要在於你試圖爲你的容器視圖定義一個具體的高度,同時試圖將其中的元素錨定到彼此及其邊緣。我最終使用了使用佈局錨點的標準Auto Layout 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 
    } 
} 
相關問題