// UIStackView using .fillProportionally
class ProportionalStackVC: UIViewController {
let proLeft: UILabel = {
let v = UILabel()
v.textAlignment = .left
v.lineBreakMode = .byTruncatingTail
v.backgroundColor = .cyan
return v
}()
let proRight: UILabel = {
let v = UILabel()
v.textAlignment = .right
v.lineBreakMode = .byTruncatingHead
v.backgroundColor = .yellow
return v
}()
let sampleStrings: [[String]] = [
["My text is less than half.", "Less than half."],
["Less than half.", "My text is less than half."],
["This text is a bit wider than half.", "Less than half."],
["Less than half.", "This text is a bit wider than half."],
["This text is considerably wider than half.", "Less than half."],
["Less than half.", "This text is considerably wider than half."],
["This text is considerably wider than half.", "Text still Less than half."],
["Text still Less than half.", "This text is considerably wider than half."],
["The Left Label is considerably wider than half.", "Right Label is wider than half."],
["Left Label is wider than half.", "The Right Label is considerably wider than half."],
["This text is just about but not quite full width.", "Short."],
["Short.", "This text is just about but not quite full width."],
]
var idx: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
let stackView = UIStackView()
stackView.distribution = .fillProportionally
stackView.spacing = 10
stackView.addArrangedSubview(proLeft)
stackView.addArrangedSubview(proRight)
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
stackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 8.0),
stackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -8.0),
])
updateLabels()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
updateLabels()
}
func updateLabels() {
let p = sampleStrings[idx % sampleStrings.count]
proLeft.text = p[0]
proRight.text = p[1]
idx += 1
if idx == sampleStrings.count {
idx = 0
}
}
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎