Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the dynamic height of UIView which contains two or more UILabels vertically?

Tags:

ios

swift

I am trying to find the dynamic height of a UIView at the run time. So, for this I calculated the run time height of UILabels in it and the constraint constants and add them all to get the desired height. But this doesn't seems to be the correct way of doing it, because in case I alter the count of UILabel or constraints in future then I do have to change the code to calculate the dynamic height. So, is there any way to get the height of UIView at run time with any number of labels and constraints in it?

MY CODE

Here uiview is UIView of which I need to calculate the dynamic height, label1 and label2 are the UILabels in which I inserted text programatically. height is a string extension to find dynamic height of UILabel. label1TopConstraint, label1BottonContraint, label2BottomConstraint are constraints IBOutlets.

let label1Height = label1.text!.height(withConstrainedWidth: label1.frame.width, font: label1.font)
let label2Height = label2.text!.height(withConstrainedWidth: label2.frame.width, font: label2.font)
uiviewHeight = label1Height + label2Height + label1TopConstraint.constant + label1BottonContraint.constant + label2BottomConstraint.constant
like image 709
Tanmay Agarwal Avatar asked Dec 19 '25 21:12

Tanmay Agarwal


1 Answers

Why do you need those runtime calculations if you already use AutoLayout and can rely on constraints? Here's an example:

A view, containing two labels:

import UIKit

final class LabelsView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .red

        let label1 = UILabel()
        label1.translatesAutoresizingMaskIntoConstraints = false
        label1.text = "Label 1"
        label1.backgroundColor = .red

        addSubview(label1)
        NSLayoutConstraint.activate([label1.widthAnchor.constraint(equalTo: widthAnchor),
                                     label1.centerXAnchor.constraint(equalTo: centerXAnchor),
                                     label1.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.5),
                                     label1.topAnchor.constraint(equalTo: topAnchor)])

        let label2 = UILabel()
        label2.translatesAutoresizingMaskIntoConstraints = false
        label2.text = "Label 2"
        label2.backgroundColor = .green

        addSubview(label2)
        NSLayoutConstraint.activate([label2.widthAnchor.constraint(equalTo: widthAnchor),
                                     label2.centerXAnchor.constraint(equalTo: centerXAnchor),
                                     label2.heightAnchor.constraint(equalTo: label1.heightAnchor),
                                     label2.bottomAnchor.constraint(equalTo: bottomAnchor)])
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

An example usage outcome:

enter image description here

Here's a view that reuses the first one (among others):

final class AnotherView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .blue

        let labelsView = LabelsView()
        labelsView.translatesAutoresizingMaskIntoConstraints = false

        addSubview(labelsView)
        NSLayoutConstraint.activate([labelsView.widthAnchor.constraint(equalTo: widthAnchor),
                                     labelsView.centerXAnchor.constraint(equalTo: centerXAnchor),
                                     labelsView.topAnchor.constraint(equalTo: topAnchor, constant: 10.0)])

        let yetAnotherView = UIView()
        yetAnotherView.translatesAutoresizingMaskIntoConstraints = false
        yetAnotherView.backgroundColor = .white

        addSubview(yetAnotherView)
        NSLayoutConstraint.activate([yetAnotherView.widthAnchor.constraint(equalTo: widthAnchor),
                                     yetAnotherView.centerXAnchor.constraint(equalTo: centerXAnchor),
                                     yetAnotherView.heightAnchor.constraint(equalTo: labelsView.heightAnchor),
                                     yetAnotherView.topAnchor.constraint(equalTo: labelsView.bottomAnchor,
                                                                         constant: 10.0)])
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

An example usage output:

enter image description here

like image 165
lazarevzubov Avatar answered Dec 22 '25 11:12

lazarevzubov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!