Search code examples
arraysswiftuilabeliboutletcollection

How to add a border all labels to an array of labels in an outlet collection in Swift?


class ViewController: UIViewController {

    @IBOutlet var labels: [UILabel]!


    override func viewDidLoad() {
        super.viewDidLoad()


        labels[0].layer.borderWidth = 1
        labels[0].layer.borderColor = UIColor.black.cgColor
    }
}

Solution

  • You can loop through the labels and set the border as below,

    labels.forEach { (label) in
        label.layer.borderColor = UIColor.black.cgColor
        label.layer.borderWidth = 1.0
    }