gpt4 book ai didi

ios - 为什么 sizeThatFits 不能与 UILabel 一起使用?

转载 作者:行者123 更新时间:2023-11-30 12:08:04 26 4
gpt4 key购买 nike

我有一个简单的类,其中包含两个标签和堆栈中的一行:

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)
}
}

当我调用 sizeThatFits 时,它只是将高度返回给我:

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

如何计算给定宽度所需的高度?

最佳答案

我相信你想要.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

我没有您用于 .constrain_edges 的任何东西,所以这是我正在运行的实际 View 类:

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

}
}

关于ios - 为什么 sizeThatFits 不能与 UILabel 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com