Different Height for same UILabel Text

女生的网名这么多〃 提交于 2020-06-29 03:47:55

问题


I have two UILabel with (orange and pink one) one is multiline label and other one is just one label. Two of this component (which has same texts) is inside UIStackView and this Stackview is inside UITableView (Automatic dimension). My problem is that whether I set the same text to UILabels their heights look different. I'm searching for this problem for two days but none of solutions work for me.

Ambiguous layout warnings for UILabels in UITableViewCell

Label1 has top, left, right layout and Label2 has top,left,right,bottom. UIStackView distribution is .fillProportionally

I want the labels has same height If they are same for example;

For View1;

  • Label1 = "Multiline Label 1 Text which is two line"

  • Label2 = "One Line Label 2 text"

    For View2;

  • Label1 = "Multiline Label 1 Text which is two line"

  • Label2 = "One Line Label 2 text"

What I want is: view1 and view2 height should be same because they have same elements with same content In them. Stackview which has this view1 and view2 has fill proportionally option should set Its height according to view1 + view2 height.

Container view which gets a xib file (It is just a xib file which has two label in it with the constraints I mention earlier).

class ViewWithLabel: UIView {

@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!
@IBOutlet var containerView: UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    
    self.translatesAutoresizingMaskIntoConstraints = false
    
    // first: load the view hierarchy to get proper outlets
    let name = String(describing: type(of: self))
    let nib = UINib(nibName: name, bundle: .main)
    nib.instantiate(withOwner: self, options: nil)
    
    addSubview(containerView)
    containerView.frame = self.bounds
    containerView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    
}

convenience init(firstLabelText: String, secondLabelText: String) {
    self.init()
    firstLabel.text = firstLabelText
    secondLabel.text = secondLabelText
}

override func layoutSubviews() {
    super.layoutSubviews()
    firstLabel.preferredMaxLayoutWidth = self.frame.width
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

Another container view which has a Stackview and gets the above view and add to Stackview.

    class ViewWithStack: UIView {

    let verticalStackView: UIStackView = {
           let s = UIStackView()
           s.distribution = .fillEqually
           s.spacing = 0
           s.axis = .vertical
           s.translatesAutoresizingMaskIntoConstraints = false
           return s
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.translatesAutoresizingMaskIntoConstraints = false
        self.backgroundColor = UIColor.white
        self.layer.cornerRadius = 6.0
        self.layer.applySketchShadow(color: UIColor(red:0.56, green:0.56, blue:0.56, alpha:1), alpha: 0.2, x: 0, y: 0, blur: 10, spread: 0)
        
        addSubview(verticalStackView)
        verticalStackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        let bottomEqual = verticalStackView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0)
        bottomEqual.isActive = true
        verticalStackView.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
        verticalStackView.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 0).isActive = true
        verticalStackView.layoutMargins = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        verticalStackView.isLayoutMarginsRelativeArrangement = true
    }

    convenience init(orientation: NSLayoutConstraint.Axis,labelsArray: [UIView]) {
        self.init()
        verticalStackView.axis = orientation
        for label in labelsArray {
            verticalStackView.addArrangedSubview(label)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    }

EDIT: If I write firstLabel.sizeToFit() in layoutSubviews function. First label looks fixed but content view still has same height and there is a big gap between label 1 and label 2.

EDIT 2: When I add setContentHuggingPriority(.required, for: .vertical) for labels. First label looks okay but second label looks bigger too.

来源:https://stackoverflow.com/questions/58413397/different-height-for-same-uilabel-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!