UIStackView 中的 UILabel

编程入门 行业动态 更新时间:2024-10-27 05:33:28
本文介绍了UIStackView 中的 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-29 03:06:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UIStackView   UILabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!