问题描述
限时送ChatGPT账号..虽然我将单个 UILabel 放入 UIStackView 的堆栈中,但一切正常.但我需要的是在 UILabel 下实际放置一个 UIView 并保持 UILabel 的自大小.我只是想知道,有没有办法在不使用 UITableView 和自定大小的单元格的情况下实现这一目标?我用过这个代码
While I put a single UILabel into the stack of UIStackView, everything works fine as expected. But what I need is to actually put a UIView under the UILabel and keep the self-size of UILabel to be kept. I just wondering, is there is a way to achieve this without using UITableView and self-sized cell? I used this code
let textLabel = UILabel()
textLabel.text = "zaciatok TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT koniec"
textLabel.numberOfLines = 0
textLabel.sizeToFit()
textLabel.backgroundColor = .yellow
let testItemView = UIView()
testItemView.backgroundColor = .green
testItemView.addSubview(textLabel)
mainStackView.addArrangedSubview(testItemView)
但它只显示一行文字,这不是我需要的东西.
but it shows the text with only one line which is not making the thing I need.
我已经用代码尝试了新的建议
I have tried the new suggestion with the code
mainStackView.distribution = .fill
let textLabel = UILabel()
textLabel.text = "zaciatok TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT koniec"
textLabel.numberOfLines = 0
textLabel.sizeToFit()
textLabel.backgroundColor = .yellow
let testItemView = UIView()
testItemView.backgroundColor = .green
testItemView.addSubview(textLabel)
mainStackView.addArrangedSubview(testItemView)
testItemView.translatesAutoresizingMaskIntoConstraints = false
testItemView.heightAnchor.constraint(equalToConstant: 50).isActive = true
我明白
当我删除 sizeToFit 时,根本没有标签我对stackview的限制:
when I remove sizeToFit, there is no label at all My constraints of stackview:
推荐答案
试试这个
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let mainStackView = UIStackView()
mainStackView.axis = .vertical
mainStackView.spacing = 3
mainStackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(mainStackView)
mainStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(10)-[mainStackView]-(10)-|", options: [], metrics: nil, views: ["mainStackView":mainStackView]))
let testItemView = UIView()
testItemView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMinXMaxYCorner,.layerMaxXMinYCorner]
testItemView.layer.cornerRadius = 15.0
testItemView.layer.masksToBounds = true
testItemView.backgroundColor = .green
testItemView.translatesAutoresizingMaskIntoConstraints = false
let textLabel = UILabel()
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.text = "zaciatok TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT koniec"
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
textLabel.backgroundColor = .yellow
testItemView.addSubview(textLabel)
testItemView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(5)-[textLabel(>=30)]-(5)-|", options: [], metrics: nil, views: ["textLabel":textLabel]))
testItemView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(5)-[textLabel]-(5)-|", options: [], metrics: nil, views: ["textLabel":textLabel]))
mainStackView.addArrangedSubview(testItemView)
}
}
这篇关于UIStackView 中的 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论