简体   繁体   English

Swift-如何动态创建标签

[英]Swift- How to dynamically create labels

I have created some code which reads through an array and saves data for each index into variables which I then pass onto to a created label. 我创建了一些代码,这些代码读取一个数组并将每个索引的数据保存到变量中,然后传递给创建的标签。

Below is the code: example of data arr content : ["2,5","5,1"] two indexes inside array 下面是代码:数据arr内容的示例: ["2,5","5,1"]数组内有两个索引

for i in 0..<dataArr.count {
let element = dataArr[i]
let labelNum = UILabel()
label split = element.components(separatedBy: ",")

let num1 = split[0]
let num2 = split[1]

let num1Nnum2 = "number 1 :" + num1 + " number 2:" + num2
labelnum.text = num1Nnum2
labelnum.textAlignment = .center
labelnum.frame = CGRect( x:10, y:90, width:250, height: 80)
self.view.addSubview(labelnum)

}

how can I create it so that when the label is created the second time when it reads index[1] it creates a new label with same code but drop the label under the first label. 我如何创建它,以便当第二次创建标签时,当它第二次读取index [1]时,它将创建一个具有相同代码的新标签,但是将标签放在第一个标签下。 I have tried to do : 我尝试做:

labelnum[i] to attempt to create a new label using the value of index for example labelnum1 when i is = 1. labelnum [i]尝试使用索引值创建新标签,例如,当i = 1时使用labelnum1。

Any help will be Appreciated. 任何帮助将不胜感激。

There is UIStackView in iOS which lets you add elements dynamically at the bottom or top of the existing views. iOS中有UIStackView ,可让您在现有视图的底部或顶部动态添加元素。 You can always add a new label which automatically appears at the bottom of the view. 您始终可以添加一个新标签,该标签会自动显示在视图底部。 You can also accomplish this with UITableView or UIScrollView . 您也可以使用UITableViewUIScrollView完成此操作。

Here is an example of UIStackView , dynamically appending new label below previous one. 这是UIStackView的示例, 动态地将新标签附加到前一个标签之下。 I hope you can infer this for your use case, 希望您可以为您的用例进行推断,

class ViewController: UIViewController {

    var lastLabelCount = 0

    var stackView: UIStackView!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        let tap = UITapGestureRecognizer(target: self,
                                         action: #selector(tapped))
        view.addGestureRecognizer(tap)
        createViews()
    }

    func createViews() {
        stackView = UIStackView(frame: .zero)
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .vertical
        stackView.alignment = .top
        view.addSubview(stackView)

        NSLayoutConstraint.activate([
            stackView.leftAnchor.constraint(equalTo: view.leftAnchor),
            stackView.rightAnchor.constraint(equalTo: view.rightAnchor),
            stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            ])
    }

    @objc func tapped() {
        let label = UILabel(frame: .zero)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textColor = UIColor.black
        label.text = "Hi I am label \(lastLabelCount)"
        stackView.addArrangedSubview(label)

        lastLabelCount += 1
    }
}

Create a variable to hold Y Position of The Label. 创建一个变量以保存标签的Y位置。 and in each iteration add the height of previous label in Y Position variable to drop new label to below previous one. 并在每次迭代中,在Y Position变量中添加前一个标签的高度,以将新标签降低到前一个标签的下方。

class ViewController: UIViewController {

let dataArr = ["2,5","5,1"]
override func viewDidLoad() {
    super.viewDidLoad()
    var yPos = 90
    for i in 0..<dataArr.count {
        let element = dataArr[i]
        let labelNum = UILabel()
        let split = element.components(separatedBy: ",")

        let num1 = split[0]
        let num2 = split[1]

        let num1Nnum2 = "number 1 :" + num1 + " number 2:" + num2
        labelNum.text = num1Nnum2
        labelNum.textAlignment = .center
        labelNum.frame = CGRect( x:10, y:yPos, width:250, height: 80)
        yPos += 80
        self.view.addSubview(labelNum)

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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