简体   繁体   中英

Why doesn't sizeThatFits work with UILabel?

I have a simple class that contains two labels and a line in a stack:

class TestView: UIView
{
    let label_A     = UILabel()
    let label_B     = UILabel()

    override init(frame: CGRect)            { super.init(frame: frame);     setup() }
    required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder);  setup() }
    func setup()
    {
        let view_BG     = UIView()
        let view_LineH  = UIView()

        // Configure
        view_BG.backgroundColor = .white
        view_BG.layer.cornerRadius = 6

        view_LineH.backgroundColor = .gray

        label_A.numberOfLines = 0
        label_A.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
        label_A.textColor = .red
        label_B.numberOfLines = 0
        label_B.textColor = .blue
        label_B.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.2)

        label_A.text = "TestA"
        label_B.text = "Experiment with Swift standard library types and learn high-level concepts using visualizations and practical examples. Learn how the Swift standard library uses protocols and generics to express powerful constraints. Download the playground below to get started."

        // Assemble
        self.addSubview(view_BG)
        view_BG.addSubview(label_A)
        view_BG.addSubview(view_LineH)
        view_BG.addSubview(label_B)

        // Layout
        view_BG.constrain_edges(to: self)
        label_A.constrain_edges(to: view_BG, excludingEdge: .bottom)
        label_B.constrain_edges(to: view_BG, excludingEdge: .top)

        view_LineH.constrain_height(1)
        view_LineH.constrain_left(to: view_BG)
        view_LineH.constrain_right(to: view_BG)
        view_LineH.constrain_topToBottom(of: label_A)
        label_B.constrain_topToBottom(of: view_LineH)
    }
}

When I call sizeThatFits, it just spits the height back at me:

let v = TestView()
let s = v.sizeThatFits(CGSize(width: 200, height: 10000))
// s is (200, 10000)

How can I calculate the desired height with a given width?

I believe you want .systemLayoutSizeFitting() :

    let tv = TestView()

    let targSize = CGSize(width: 200, height: 10000)

    let fitSize = tv.systemLayoutSizeFitting(targSize, withHorizontalFittingPriority: 1000, verticalFittingPriority: 1)

    print(fitSize)

    // prints "(200.0, 245.0)" in my playground

I don't have whatever you are using for .constrain_edges stuff, so here is the actual view class I'm running:

class TestView: UIView
{
    let label_A     = UILabel()
    let label_B     = UILabel()

    override init(frame: CGRect)            { super.init(frame: frame);     setup() }
    required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder);  setup() }
    func setup()
    {
        let view_BG     = UIView()
        let view_LineH  = UIView()

        // Configure
        view_BG.backgroundColor = .white
        view_BG.layer.cornerRadius = 6

        view_LineH.backgroundColor = .gray

        label_A.numberOfLines = 0
        label_A.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
        label_A.textColor = .red
        label_B.numberOfLines = 0
        label_B.textColor = .blue
        label_B.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.2)

        label_A.text = "TestA"
        label_B.text = "Experiment with Swift standard library types and learn high-level concepts using visualizations and practical examples. Learn how the Swift standard library uses protocols and generics to express powerful constraints. Download the playground below to get started."

        // Assemble
        self.addSubview(view_BG)
        view_BG.addSubview(label_A)
        view_BG.addSubview(view_LineH)
        view_BG.addSubview(label_B)

        view_BG.translatesAutoresizingMaskIntoConstraints = false
        label_A.translatesAutoresizingMaskIntoConstraints = false
        label_B.translatesAutoresizingMaskIntoConstraints = false
        view_LineH.translatesAutoresizingMaskIntoConstraints = false

        // Layout
        view_BG.topAnchor.constraint(equalTo: self.topAnchor, constant: 0.0).isActive = true
        view_BG.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0.0).isActive = true
        view_BG.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0.0).isActive = true
        view_BG.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0).isActive = true


        label_A.topAnchor.constraint(equalTo: view_BG.topAnchor, constant: 0.0).isActive = true
        label_A.leftAnchor.constraint(equalTo: view_BG.leftAnchor, constant: 0.0).isActive = true
        label_A.rightAnchor.constraint(equalTo: view_BG.rightAnchor, constant: 0.0).isActive = true

        label_B.leftAnchor.constraint(equalTo: view_BG.leftAnchor, constant: 0.0).isActive = true
        label_B.rightAnchor.constraint(equalTo: view_BG.rightAnchor, constant: 0.0).isActive = true
        label_B.bottomAnchor.constraint(equalTo: view_BG.bottomAnchor, constant: 0.0).isActive = true

        view_LineH.leftAnchor.constraint(equalTo: view_BG.leftAnchor, constant: 0.0).isActive = true
        view_LineH.rightAnchor.constraint(equalTo: view_BG.rightAnchor, constant: 0.0).isActive = true

        view_LineH.topAnchor.constraint(equalTo: label_A.bottomAnchor, constant: 0.0).isActive = true
        label_B.topAnchor.constraint(equalTo: view_LineH.bottomAnchor, constant: 0.0).isActive = true

        view_LineH.heightAnchor.constraint(equalToConstant: 1.0).isActive = true

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM